-
Notifications
You must be signed in to change notification settings - Fork 636
Description
First and foremost - thanks for the great work on the package, the API is fantastic!
I commonly build population pyramids and I'd like to use Plotly to do this as well. However, I'm running into a couple issues when doing this, so I thought I'd see if anyone had some feedback on these issues.
Normally when setting up a population pyramid I set the population values for one of the genders to negative numbers to built a "pyramid" around a central axis. For example:
library(plotly)
age <- rep(1:5, 2)
sex <- rep(c('Male', 'Female'), each = 5)
pop <- c(-1000, -944, -888, -762, -667, 1100, 999, 844, 789, 655)
df <- data.frame(age, sex, pop)
plot_ly(df, x = pop, y = age, group = sex, type = 'bar', orientation = 'h')On the right track here, but I want the bars to align, as in a stacked bar chart. When I change the barmode to 'stack', I get this:
plot_ly(df, x = pop, y = age, group = sex, type = 'bar', orientation = 'h') %>%
layout(barmode = 'stack')Alternatively, I also tried adding the genders as separate traces, but Plotly then plots positive numbers as negative:
library(dplyr)
male <- filter(df, sex == 'Male')
female <- filter(df, sex == 'Female')
plot_ly(male, x = pop, y = age, type = 'bar', orientation = 'h') %>%
add_trace(female, type = 'bar', x = pop, y = age, orientation = 'h') %>%
layout(barmode = 'stack')If you have any ideas about this, please let me know!
Also: when creating population pyramids, I want to display all of the axis values as absolute values on either side of zero. In other R/JS libraries, I've passed something like the following to tickformat:
function(d) { return d3.format(',.1f')(Math.abs(d)) }I've tried this out in Plotly, both as a string and wrapped in htmlwidgets::JS(), like this:
plot_ly(df, x = pop, y = age, group = sex, type = 'bar', orientation = 'h') %>%
layout(xaxis = list(
tickformat = htmlwidgets::JS("function(d) { return d3.format(',.0f')(Math.abs(d)) }")
))and I don't get it to change. I'll keep working on this, but please let me know if you have any pointers!


