图例不匹配数据
Legend mismatch data
我正在尝试使用这段代码,但遇到了 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")
请在此处查找结果图和数据
Data and plot
ggplot 只是绘制数据中的内容 - 您需要检查和清理数据。看看data.frame中"crop"的不同等级:
> levels(scdata$Crop)
[1] "" "alfalfa" "corn" "oat" "soybean"
这就是为什么图例中出现 "extra" 空白裁剪的原因。您可以看到相关行:
scdata[scdata$Crop=="",]
这将显示您已阅读 CSV 文件末尾的一组 summary/total/blank 行。
我还建议不要直接在 ggplot 调用中设置 x 轴标签,尤其是当您有很多标签时。它很容易导致错误标记问题,如果数据不符合您的预期顺序,将很难发现。我不知道你想要什么字段,但是使用 paste
和 sprintf
之类的东西提前设置你的标签。
我正在尝试使用这段代码,但遇到了 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")
请在此处查找结果图和数据 Data and plot
ggplot 只是绘制数据中的内容 - 您需要检查和清理数据。看看data.frame中"crop"的不同等级:
> levels(scdata$Crop)
[1] "" "alfalfa" "corn" "oat" "soybean"
这就是为什么图例中出现 "extra" 空白裁剪的原因。您可以看到相关行:
scdata[scdata$Crop=="",]
这将显示您已阅读 CSV 文件末尾的一组 summary/total/blank 行。
我还建议不要直接在 ggplot 调用中设置 x 轴标签,尤其是当您有很多标签时。它很容易导致错误标记问题,如果数据不符合您的预期顺序,将很难发现。我不知道你想要什么字段,但是使用 paste
和 sprintf
之类的东西提前设置你的标签。