Little Bee
Little Bee

Reputation: 1225

How to label x-axis in ggplot when using facets

I was trying to change the x-axis on my plot with the code below. I named the desired labels on scale_x-discrete in orders corresponding to the legend to the right of the plot but they ended up messy. Corn appeared twice while Oat was missing for "Three-year", then Corn appeared twice again in "Four-year" with Alfalfa missing. Labels were mixed up for "Three-year" and "Four-year" too.

data$rotation[data$Rot.trt %in% c("C2", "S2")]<-"TwoYear"
data$rotation[data$Rot.trt %in% c("C3", "S3", "O3")]<-"ThreeYear"
data$rotation[data$Rot.trt %in% c("C4", "S4", "O4", "A4")]<-"FourYear"

##plot, by rotation #scales = free_x X axis depends on facet
data$rotation <- factor(data$rotation, levels = c("TwoYear", "ThreeYear", "FourYear"))
ggplot(data, aes(Rot.Herb, kg.ha, fill=Crop))+
  geom_boxplot()+
  facet_grid(~rotation, scales = "free_x", space="free_x")+
  scale_fill_brewer(palette = "Paired")+
  ggtitle("Weed biomass by plot")+
  theme(plot.title = element_text(size=30, face="bold", vjust=2))+
  xlab("Rotation systems and Herbicide regimes (L = Low herbicide regime, C = Conventional herbicide regime)")+
  scale_x_discrete(labels = c("Corn C", "Corn L", "Soybean C", "Soybean L", "Corn C", "Corn L", "Oat C", "Oat L", "Soybean C", "Soybean L", "Alfalfa C", "Alfalfa L", "Corn C", "Corn L", "Oat C", "Oat L", "Soybean C", "Soybean L"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  ylab("Weed dry weight")

Please find picture and data here:

https://www.dropbox.com/sh/jb6gjznyw2q16mx/AADcNKiicqkoHxpFYIsaTgk9a?dl=0

enter image description here

Thank you!

Upvotes: 5

Views: 10861

Answers (1)

jan zegan
jan zegan

Reputation: 1657

Instead of scale_x_discrete you can map Rot.Herb values into your axis labels using mapvalues from plyr package and then group on that. I'm not sure if I got the labels completely right, but something along those lines

...
library(plyr)
data$Rot.Herb.label <- mapvalues(data$Rot.Herb, 
          c('C2conv', 'C2low', 'S2conv', 'S2low', 'C3conv', 'C3low',
            'O3conv', 'O3low', 'S3conv', 'S3low', 'A4conv', 'A4low',
            'C4conv', 'C4low', 'O4conv', 'O4low', 'S4conv', 'S4low'),
          c("Corn C", "Corn L", "Soybean C", "Soybean L", 
            "Corn C", "Corn L", "Oat C", "Oat L", "Soybean C", 
            "Soybean L", "Alfalfa C", "Alfalfa L", "Corn C", "Corn L",
            "Oat C", "Oat L", "Soybean C", "Soybean L"))


ggplot(data, aes(Rot.Herb.label, kg.ha, fill=Crop))+
  geom_boxplot()+
  facet_grid(~rotation, scales = "free_x", space="free_x")+
  scale_fill_brewer(palette = "Paired")+
  ggtitle("Weed biomass by plot")+
  theme(plot.title = element_text(size=30, face="bold", vjust=2))+
  xlab("Rotation systems and Herbicide regimes (L = Low herbicide regime, C = Conventional herbicide regime)")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  ylab("Weed dry weight")
...

That produces enter image description here

Upvotes: 2

Related Questions