简体   繁体   中英

Rotating x label text in ggplot

The code below should rotate and align the text labels on the x-axis, but for some reason it don't:

ggplot(res, aes(x=TOPIC,y=count), labs(x=NULL)) + 
  scale_y_continuous(limits=c(0,130),expand=c(0,0)) +
  scale_x_discrete("",labels=c("ANA"="Anatomy","BEH"="Behavior","BOUND"="Boundaries",
                           "CC"="Climate change","DIS"="Disease","EVO"="Evolution",
                           "POPSTAT"="Pop status","POPABU"="Pop abundance",
                           "POPTR"="Pop trend","HARV"="Harvest","HAB"="Habitat",
                           "HABP"="Habitat protection","POLL"="Pollution",
                           "ZOO"="Captivity","SHIP"="Shipping","TOUR"="Tourism",
                           "REPEC"="Reprod ecology","PHYS"="Physiology","TEK"="TEK",
                           "HWC"="HWC","PRED"="Predator-prey","METH"="Methods",
                           "POPGEN"="Pop genetics","RESIMP"="Research impact",
                           "ISSUE"="Other","PROT"="Protection","PA"="Protected areas",
                           "PEFF"="Protection efficiency","MINOR"="Minor")) + 
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_bar(stat='identity') +
  theme_bw(base_size = 16) +
  ggtitle("Peer-reviewed papers per topic")

You need to change the order of the layers, otherwise theme_bw will override theme :

ggplot(res, aes(x=TOPIC,y=count), labs(x=NULL)) + 
  scale_y_continuous(limits=c(0,130),expand=c(0,0)) +
  scale_x_discrete("",labels=c("ANA"="Anatomy","BEH"="Behavior","BOUND"="Boundaries",
                           "CC"="Climate change","DIS"="Disease","EVO"="Evolution",
                           "POPSTAT"="Pop status","POPABU"="Pop abundance",
                           "POPTR"="Pop trend","HARV"="Harvest","HAB"="Habitat",
                           "HABP"="Habitat protection","POLL"="Pollution",
                           "ZOO"="Captivity","SHIP"="Shipping","TOUR"="Tourism",
                           "REPEC"="Reprod ecology","PHYS"="Physiology","TEK"="TEK",
                           "HWC"="HWC","PRED"="Predator-prey","METH"="Methods",
                           "POPGEN"="Pop genetics","RESIMP"="Research impact",
                           "ISSUE"="Other","PROT"="Protection","PA"="Protected areas",
                           "PEFF"="Protection efficiency","MINOR"="Minor")) + 
  theme_bw(base_size = 16) +
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_bar(stat='identity') +
  ggtitle("Peer-reviewed papers per topic")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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