tbowers0705
tbowers0705

Reputation: 23

Why can I not get the x axis labels to show in my graph?

This is the data (in not the most readable form):

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

This is what I am running:

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

However, I am not getting the x-axis labels "Openness","Extraversion","Conscientiousness","Agreeableness". What am I doing wrong?

Upvotes: 2

Views: 39

Answers (1)

davechilders
davechilders

Reputation: 9123

Try removing the breaks argument to 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)))

In addition to removing the breaks argument to scale_x_discrete, I also specified all your themeelements in one call to theme(). This makes your code more readable.

Upvotes: 1

Related Questions