I have a data frame which I want to display in a grouped and stacked bar chart with ggplotly.
library(ggplotly)
library(dplyr)
library(tidyr)
year = rep(c(2019,2019,2020,2020), 2)
month = rep(c("June","July"), 4)
gender = c("M","M","M","M","F","F","F","F")
male = c(11,13,15,17)
female = c(12,14,16,18)
count = c(11,13,15,17,12,14,16,18)
df <- data.frame(year, month, gender, count = as.numeric(count))
p <- df %>%
mutate(count=as.numeric(count),
month=factor(month, levels=c("June","July")),
Month=paste(year,month,sep="-")) %>%
ggplot(aes(month, count, fill=gender)) +
geom_bar(stat="identity", position=position_stack()) +
theme_minimal() + xlab("") +
facet_grid(~year, switch='x') +
theme(axis.text.x = element_text(margin = margin(t = -1, unit = "cm")))
ggplotly(p)
This works fine but when I deselect "M" in the legend the "F"-bars remain floating in their original position and do not move down to the x-axis. What am I doing wrong?
plotly
notggplotly
? – Daniel Jachetta