【发布时间】:2020-08-13 13:01:49
【问题描述】:
我有一个数据框,我想用 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)
这很好用,但是当我在图例中取消选择“M”时,“F”条保持浮动在其原始位置并且不会向下移动到 x 轴。我做错了什么?
【问题讨论】:
-
可能重复。通过使用 plot_ly 解决。见:stackoverflow.com/questions/45326425/…
-
它不是重复的,因为我的图表是分组和堆叠的。
-
不是包
plotly不是ggplotly吗?