r4fun / hierplane

🌳 Hierplane for R
https://r4fun.github.io/hierplane/
Other
9 stars 0 forks source link

Build hp df with attributes #41

Closed tylerlittlefield closed 4 years ago

tylerlittlefield commented 4 years ago

This carries the data forward by preserving an attribute source which represent the original dataset passed to add_root(). I also removed some dplyr functions to avoid depending on dplyr. Also added the pipe as a dependency since we have functions that are designed to use them. Now we can do:

devtools::load_all()

df <- structure(
  list(
    Survey = c(
      "OS Students 2014/15",
      "OS Students 2014/15",
      "OS Students 2014/15",
      "OS Students 2014/15",
      "OS Students 2014/15",
      "OS Students 2014/15",
      "OS Students 2014/15"
    ),
    `Operating System` = c("OS X",
                           "OS X", "Linux", "Linux", "Windows", "Windows", "Windows"),
    `OS Version` = c(
      "Yosemite",
      "Leopard",
      "Debian",
      "Ubuntu",
      "Win7",
      "Win8",
      "Win10"
    ),
    users = c(16,
              43, 27, 36, 31, 32, 4)
  ),
  row.names = c(NA, -7L),
  class = c("data.frame")
)

df %>% 
  add_root("OS Students 2014/15") %>% 
  add_layer(
    parent_vals = "OS Students 2014/15",
    child_col = "Operating System",
    link_vals = "OS", 
    node_type_vals = "OS"
  ) %>% 
  add_layer(
    parent_col = "Operating System",
    child_col = "OS Version",
    link_vals = "Ver",
    node_type_vals = "Sub",
    attribute_cols = "users"
  ) %>% 
  hp_dataframe(
    title = "Survey Results of Most Popular OS in 2014/15",
    settings = hierplane_settings(attributes = "attribute1"),
    styles = hierplane_styles(
      link_to_positions = list(Ver = "right")
    )
  ) %>%
  hierplane()
tylerlittlefield commented 4 years ago

As I look at this example, I am a little confused about using parent_vals in the first layer and then parent_cols in the second. How do we explain this?

mathidachuk commented 4 years ago

The "Survey" column does not always exist in datasets. So let's say if it does not exist, we will need to manually define the parent value to link the top level nodes to.

In this case tho, the parent can be defined as parent_vals = "OS Students 2014/15" or parent_col = "Survey"

tylerlittlefield commented 4 years ago

Oh so the first layer could just use parent_col="Survey"? I prefer that much more because it's consistent, and you can just reference the child_col from the above layer when defining the parent_col in the next layer.

mathidachuk commented 4 years ago

Yup that's the intent! But data doesn't always come in the format we need to so I built in some flexibility :)