我有两个大型数据框,其中包含许多类字符和/或因子列。我需要通过在同一条形图上覆盖每对变量的频率来比较它们在第一和第二数据帧中所采用的值的频率。我希望能够绘制计数或比例。
我可以分别绘制每一个。
ds1 <- data.frame(var1 = as.character(c("7","10","11","4", "7","10","11","4")))
ds2 <- data.frame(var2 = c("4","4","7","7", "7","10","11","4"))
ggplot(ds1, aes(var1)) + geom_bar()
ggplot(ds2, aes(var2)) + geom_bar()
但我正在努力:
答案 0 :(得分:4)
这是一种使用半透明和重叠条形的方法。我认为将条形图彼此相邻可能会更清晰一点,如果您愿意,则将行position_identity()
更改为position_dodge()
:
library(ggplot2)
ds1 <- data.frame(var1 = as.character(c("7","10","11","4", "7","10","11","4")))
ds2 <- data.frame(var2 = c("4","4","7","7", "7","10","11","4"))
plot.df <- cbind(ds1, ds2)
plot.df <- reshape2::melt(plot.df, id.vars = NULL)
ggplot(plot.df, aes(value, group=variable, fill=variable)) +
geom_bar(position = position_identity(),
aes(y = ..prop..),
alpha=.6,
color='black')+
theme_minimal() + ggtitle("Comparing the Frequency of Categories")
ds1$variable <- "ds1"
ds2$variable <- "ds2"
names(ds1) <- names(ds2)
plot.df <- rbind(ds1, ds2)
然后从这里开始绘制。
由reprex package(v0.2.0)创建于2018-05-10。