这是数据(不是最易读的形式):
id bfi stress alcohol stroop condition
1 4239 Open 0 0 2 N
2 71538 Extra 1 0 0 N
3 8855 Con 0 1 0 N
4 558 Agree 3 0 0 N
5 23 Agree 23 1 0 N
6 5 Con 6 0 2 N
7 6240 Agree 18 1 0 N
8 314567 Agree 24 0 2 N
9 325 Open 2 9 0 N
10 24402687 Extra 8 6 7 N
11 75 Agree 0 0 2 Y
12 777 Con 0 2 1 Y
13 7808 Open 18 4 5 Y
14 110 Agree 4 5 2 Y
15 2648 Open 15 2 2 Y
16 221347 Agree 6 7 4 Y
17 7 Open 4 2 4 Y
18 614 Extra 7 3 5 Y
19 777 Con 14 0 0 Y
20 79231005 Agree 13 7 3 Y
这就是我正在运行的:
library("dplyr")
library("ggplot2")
temp<-results%>%group_by(bfi)%>%summarize(means=mean(alcohol),sems=sd(alcohol)/sqrt(length(alcohol)))
f<-ggplot(temp,aes(x=factor(bfi),y=means))+geom_bar(stat="identity", color="black",fill=c("cyan4","azure3","aquamarine","azure4"))+geom_errorbar(aes(ymax=means+sems, ymin=means-sems), width=.1)
f
f<-f+ggtitle("Alcohol Usage by Personality Trait")+
labs(x="Personality Trait", y="Alcohol Usage")+
scale_x_discrete(breaks=c("open","extra","con","agree"),labels=c("Openness","Extraversion","Conscientiousness","Agreeableness"))+
theme(plot.title=element_text(size=15,face="bold",vjust=.5))+
theme(axis.title.x=element_text(size=12,face="bold",vjust=-.25))+
theme(axis.title.y=element_text(size=12,face="bold",vjust=1))+
theme(axis.text.x=element_text(size=10,face="bold",color="black"))+
theme(axis.text.y=element_text(size=10,face="bold",color="black")) +
coord_cartesian(ylim=c(min(temp$means)-2*max(temp$sems),max(temp$means)+2*max(temp$sems)))+
theme(panel.border=element_blank(), axis.line=element_line())+
theme(panel.grid.major.x=element_blank())+
theme(panel.grid.major.y=element_line(color="darkgrey"))+
theme(panel.grid.minor.y=element_blank())
f
然而,我没有得到x轴标签“开放性”,“外向性”,“责任心”,“宜人性”。我做错了什么?
答案 0 :(得分:1)
尝试将breaks
参数移至scale_x_discrete
:
f + ggtitle("Alcohol Usage by Personality Trait")+
labs(x="Personality Trait", y="Alcohol Usage")+
scale_x_discrete(
# breaks=c("open","extra","con","agree"),
labels=c("Openness","Extraversion","Conscientiousness","Agreeableness")
) +
theme(
plot.title=element_text(size=15,face="bold",vjust=.5),
axis.title.x=element_text(size=12,face="bold",vjust=-.25),
axis.title.y=element_text(size=12,face="bold",vjust=1),
axis.text.x=element_text(size=10,face="bold",color="black"),
axis.text.y=element_text(size=10,face="bold",color="black"),
panel.border=element_blank(), axis.line=element_line(),
panel.grid.major.x=element_blank(),
panel.grid.major.y=element_line(color="darkgrey"),
panel.grid.minor.y=element_blank()
) +
coord_cartesian(ylim=c(min(temp$means)-2*max(temp$sems),max(temp$means)+2*max(temp$sems)))
除了将breaks
参数移至scale_x_discrete
之外,我还在theme
的一次调用中指定了所有theme()
个参数。这使您的代码更具可读性。