问题描述
我有以下数据:
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,show_guide = FALSE)