我想要两个包含多个水平条形图的图表,每个图表都显示两个变量之一的平均值:恐惧和期望。条形图应按假人分组。
我创建了单个条形图,其中包含按每个假人分组的恐惧和期望的平均值,但我不知道如何将它们正确组合。
x = data.frame(
id = c(1, 2, 3, 4, 5),
sex = c(1, 0, 1, 0, 1),
migration = c(0, 1, 0, 1, 0),
handicap = c(0, 1, 1, 1, 0),
east = c(0, 1, 1, 1, 0),
fear = c(1, 3, 4, 6, 3),
expectation = c(2, 3, 2, 5, 4))
我想让它基本上看起来像这样:https : //ibb.co/3fz0GQ4
任何帮助将不胜感激。
要获得您显示的图,您需要对数据进行一些调整:
library(tidyverse)
x2 <- x%>%
gather(fear, expectation, key = "group", value = "value")%>%
gather(sex, migration, handicap, east, key = "dummies", value = "dum_value")%>%
group_by(group, dummies, dum_value)%>%
summarize(prop = mean(value))
然后你可以很容易地得到情节:
x2%>%
ggplot(aes(y= prop, x = dummies, fill = factor(dum_value)))+
geom_bar(stat = "identity", position = "dodge")+
coord_flip()+
facet_wrap(~group)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句