使用构面时如何在ggplot中标记x轴
How to label x-axis in ggplot when using facets
我试图用下面的代码更改绘图上的 x 轴。我按照与图例右侧的图例相对应的顺序在 scale_x-discrete 上命名了所需的标签,但它们最终变得一团糟。玉米出现两次,而燕麦在 "Three-year" 中缺失,然后玉米在 "Four-year" 中再次出现两次,而紫花苜蓿缺失。 "Three-year" 和 "Four-year" 的标签也混淆了。
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")
请在此处查找图片和数据:
https://www.dropbox.com/sh/jb6gjznyw2q16mx/AADcNKiicqkoHxpFYIsaTgk9a?dl=0
谢谢!
您可以使用 plyr
包中的 mapvalues
将 Rot.Herb
值映射到您的轴标签中,而不是 scale_x_discrete
,然后对其进行分组。我不确定我的标签是否完全正确,但大致如此
...
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")
...
产生
我试图用下面的代码更改绘图上的 x 轴。我按照与图例右侧的图例相对应的顺序在 scale_x-discrete 上命名了所需的标签,但它们最终变得一团糟。玉米出现两次,而燕麦在 "Three-year" 中缺失,然后玉米在 "Four-year" 中再次出现两次,而紫花苜蓿缺失。 "Three-year" 和 "Four-year" 的标签也混淆了。
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")
请在此处查找图片和数据:
https://www.dropbox.com/sh/jb6gjznyw2q16mx/AADcNKiicqkoHxpFYIsaTgk9a?dl=0
谢谢!
您可以使用 plyr
包中的 mapvalues
将 Rot.Herb
值映射到您的轴标签中,而不是 scale_x_discrete
,然后对其进行分组。我不确定我的标签是否完全正确,但大致如此
...
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")
...
产生