繁体   English   中英

ggplot2 箱线图中的均值符号

[英]ggplot2 mean symbol in boxplots

我有以下数据:

GENDER Addressee_gender_and_age likelihood
 Female                      F20          4
 Female                      F20          5
   Male                      F20          3
 Female                      F20          3
 Female                      F20          4
   Male                      F20          1

我有兴趣获得箱线图

p = ggplot(data = melteddata, aes(x=Addressee_gender_and_age, y=likelihood)) +
  ggtitle("Distribution of the likelihood of complaining by gender") +
  theme(plot.title = element_text(hjust = 0.5)) + 
  geom_boxplot(aes(fill=GENDER))
p + facet_wrap( ~ Addressee_gender_and_age, scales="free") +
  stat_summary(fun=mean, colour="darkred", geom="point", 
               shape=18, size=3,show_guide = FALSE)

问题是整个包装的平均符号如下:

输出

问题是您将错误的值设置为 x 轴。 我首先创建一个可重现的示例( https://stackoverflow.com/help/minimal-reproducible-example ),然后将aes(x=age, y=x)更改为aes(x=gender, y=x)在您的例如它将是GENDER而不是Addressee_gender_and_age

Test<-data.frame(x=rnorm(40),age=rep(c(10,20,30,40,50),8),
                 gender=rep(c("Male","Female"),20))

library(ggplot2)

ggplot(data = Test, aes(x=age, y=x)) +
  geom_boxplot(aes(fill=gender))+ 
  facet_wrap( ~ age, scales="free") +
  stat_summary(fun=mean, colour="darkred", geom="point", 
               shape=18, size=3,show_guide = FALSE)

在此处输入图片说明

ggplot(data = Test, aes(x=gender, y=x)) +
  geom_boxplot(aes(fill=gender))+ 
  facet_wrap( ~ age, scales="free") +
  stat_summary(fun=mean, colour="darkred", geom="point", 
               shape=18, size=3,show_guide = FALSE)

在此处输入图片说明

暂无
暂无

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

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