48

How can I change the names of my x axis labels in ggplot2? See below:

ggbox <- ggplot(buffer, aes(SampledLUL, SOC)) + geom_boxplot()

ggbox <- ggbox + theme(axis.text.x=element_text(color = "black", size=11, angle=30, vjust=.8, hjust=0.8)) 

ggbox<- ggbox + labs(title = "Land cover Classes") + ylab("SOC (g C/m2/yr)") + xlab("Land cover classes")

The above code creates the following figure: enter image description here

I would like to be able to capitilize the first letter of these classes (i.e Crop, as opposed to crop).

I've tried the code below but not sure where to put it and exactly what function to use. labels = c("Citrus", "Crop", "Cypress Swamp", ..........)

(I'm using windows 7, Rstudio)

derelict
  • 3,657
  • 3
  • 24
  • 29
  • 3
    note that `str_to_sentence` can be used to produce the desired capitalization scheme. https://stringr.tidyverse.org/reference/case.html – flies Jan 02 '20 at 20:49

1 Answers1

85

create labels:

 SoilSciGuylabs <- c("Citrus", "Crop", "Cypress Swamp")

then add:

+ scale_x_discrete(labels= SoilSciGuylabs)
user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • 7
    I used this to mask a continuous variable as a categorical so I could use geom_line. To make the labels appear I needed to set breaks first. I used `scale_x_continuous(breaks=seq(1,12,1),labels=my_labels)`. Just noting that here in case it helps someone else. – Alison Bennett Aug 09 '22 at 01:23