ox-it / oidnChaRts

A collection of functions for building generic interactive htmlwidget visualisations. A complementary guide to htmlwidgets is developed here:
http://ox-it.github.io/OxfordIDN_htmlwidgets
MIT License
5 stars 3 forks source link

Add a population_pyramid function #42

Open charliejhadley opened 7 years ago

charliejhadley commented 7 years ago

population_pyramid makes a pairwise barchart allow comparison of two variables across a number of observations.

Here's a highcharter example:

pyramid_data <- structure(list(total.projects = c(7.6656596537564, 7.6656596537564, 
7.6656596537564, 7.6656596537564, 7.6656596537564, 7.6656596537564, 
3.74825883246803, 3.74825883246803, 3.74825883246803, 3.74825883246803, 
3.74825883246803, 3.74825883246803, 4.11910490421317, 4.11910490421317, 
4.11910490421317, 4.11910490421317, 4.11910490421317, 4.11910490421317, 
0.827619891821487, 0.827619891821487, 0.827619891821487, 0.827619891821487, 
0.827619891821487, 0.827619891821487, 0.229743663959189, 0.229743663959189, 
0.229743663959189, 0.229743663959189, 0.229743663959189, 0.229743663959189, 
0.0759782195770546, 0.0759782195770546, 0.0759782195770546, 0.0759782195770546, 
0.0759782195770546, 0.0759782195770546, 0.00180900522802511), 
    category = c("Asia", "Asia", "Asia", "Asia", "Asia", "Asia", 
    "Europe", "Europe", "Europe", "Europe", "Europe", "Europe", 
    "North America", "North America", "North America", "North America", 
    "North America", "North America", "Africa", "Africa", "Africa", 
    "Africa", "Africa", "Africa", "South America", "South America", 
    "South America", "South America", "South America", "South America", 
    "Oceania", "Oceania", "Oceania", "Oceania", "Oceania", "Oceania", 
    "Netherlands Antilles"), total.workers = c(8.21693826799704, 
    8.21693826799704, 8.21693826799704, 8.21693826799704, 8.21693826799704, 
    8.21693826799704, 4.59954332874319, 4.59954332874319, 4.59954332874319, 
    4.59954332874319, 4.59954332874319, 4.59954332874319, 2.80143493544363, 
    2.80143493544363, 2.80143493544363, 2.80143493544363, 2.80143493544363, 
    2.80143493544363, 0.639043820279792, 0.639043820279792, 0.639043820279792, 
    0.639043820279792, 0.639043820279792, 0.639043820279792, 
    0.295199402942561, 0.295199402942561, 0.295199402942561, 
    0.295199402942561, 0.295199402942561, 0.295199402942561, 
    0.114491497757798, 0.114491497757798, 0.114491497757798, 
    0.114491497757798, 0.114491497757798, 0.114491497757798, 
    9.24810159594489e-05)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -37L), .Names = c("total.projects", "category", 
"total.workers")) %>% unique()

highchart() %>%
  hc_add_series(pyramid_data,
                "bar",
                hcaes(y = total.projects,
                      x = category),
                name = "Projects") %>%
  hc_add_series(pyramid_data,
                "bar",
                hcaes(y = -1 * total.workers,
                      x = category),
                name = "Workers") %>%
  hc_plotOptions(series = list(stacking = "normal"),
                 bar = list(minPointLength = 3)) %>%
  hc_xAxis(
    list(
      categories = pyramid_data$category,
      reversed = TRUE,
      opposite = FALSE
    ),
    list(
      opposite = TRUE,
      reversed = TRUE,
      categories = pyramid_data$category,
      linkedTo = 0
    )
  ) %>%
  hc_yAxis(labels = list(
    formatter = JS("function () {
                   return Math.abs(this.value) + '%';
                   }")
  )) %>%
  hc_tooltip(
    formatter = JS(
      "function () {
      return '<b>' + this.series.name + ' in ' + this.point.category + '</b><br/>' +
      Highcharts.numberFormat(Math.abs(this.point.y), 2);
      }"
)
    )

image