我正在尝试使用此代码但遇到2个问题: 1 - 传奇中的额外裁剪 2 - 不匹配的传奇
#datalocation
scdata=read.csv("SeedcountR.csv")
library(ggplot2)
library(RColorBrewer)
##Group the data by Rotation
scdata$rotation[scdata$Rot.Trt %in% c("C2", "S2")]<-"TwoYear"
scdata$rotation[scdata$Rot.Trt %in% c("C3", "S3", "O3")]<-"ThreeYear"
scdata$rotation[scdata$Rot.Trt %in% c("C4", "S4", "O4", "A4")]<-"FourYear"
##Plot
scdata$rotation <- factor(scdata$rotation, levels = c("TwoYear", "ThreeYear", "FourYear"))
ggplot(scdata, aes(Rot.Herb, Count, fill=Crop))+
geom_boxplot()+
facet_grid(~rotation, scales = "free_x", space="free_x")+
scale_fill_brewer(palette = "Paired")+
ggtitle("Weed seedbank by subplot")+
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 seed count")
&#13;
请在此处查找结果图和数据 Data and plot
答案 0 :(得分:1)
ggplot只是绘制数据中的内容 - 您需要检查并清理数据。看看&#34; crop&#34;的不同层次。在data.frame中:
> levels(scdata$Crop)
[1] "" "alfalfa" "corn" "oat" "soybean"
这就是为什么你有一个&#34;额外&#34;传说中的空白作物。您可以通过以下方式查看相关行:
scdata[scdata$Crop=="",]
其中显示您已在CSV文件末尾读取了一组摘要/总计/空白行。
我还建议不要在ggplot调用中直接设置x轴标签,特别是如果你有很多。如果数据不符合您的预期,它很容易导致错误标记问题,这将很难发现。我并不确切知道您想要哪些字段,但请使用paste
和sprintf
之类的内容来提前设置标签。