繁体   English   中英

如何使用 ggplot 创建两个彼此相邻的堆叠条形图。 我想重新创建下面的图表:

[英]How to create two stacked bar charts next to each other using ggplot. I want to recreate the below chart:

我有以下2个数据框:

lc2 <- structure(list(group = 1:3, sumpct = c(13, 32, 54)), class = "data.frame", row.names = c(NA, 
-3L))

请注意,这是针对“点击的可能性”栏(见图),其中“非常/有点可能”是 13%,中性是 32,非常/不太可能是 54)

le2 <- structure(list(e = 1:3, t = c(13, 38, 48)), class = "data.frame", row.names = c(NA, 
-3L))

请注意,上面的代码同样适用于下面的“注册可能性”栏。

但我想创建这个:

堆积条形图

lc2 <- structure(list(group = 1:3, sumpct = c(13, 32, 54)), 
                 class = "data.frame", row.names = c(NA, -3L))
le2 <- structure(list(e = 1:3, t = c(13, 38, 48)), 
                 class = "data.frame", row.names = c(NA, -3L))



lc2$type <- "click"
le2$type <- "enroll"

colnames(lc2) <- c("group", "pct", "type")
colnames(le2) <- c("group", "pct", "type")

library(data.table)
library(ggplot2)
dt <- rbindlist(list(lc2, le2))
dt[, group := as.factor(group)]
ggplot(dt, aes(x = type, y = pct, fill = group)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label=scales::percent(pct/100)), position = position_stack(vjust = .5))+
  theme_classic() +
  coord_flip()
  

带百分比的质押条形图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM