How to designate the x axis in ggplot when using faces

I tried to change the x axis on my graph using the code below. I called the desired labels on scale_x-discrete in the order corresponding to the legend to the right of the plot, but they turned out to be messy. Corn appeared twice, while Out was absent for Three Years, then Corn again appeared twice in Four Years with no alfalfa. Labels were also implicated for Three Years and Four Years.

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")
Run codeHide result

Please find the image and data here:

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

enter image description here

Thank!

+4
source share
1 answer

scale_x_discrete Rot.Herb mapvalues from plyr, . , , -

...
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")
...

enter image description here

+1

All Articles