2015-05-26 15 views
0

我想使用此代碼,但得到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")

enter image description here

請找結果情節和這裏的數據 Data and plot

+0

你能否澄清你的意思'2個問題:1 - 不匹配legend' - 傳說中的2額外的收成?這個描述看起來太寬泛,不知道發生了什麼。 – jazzurro

回答

1

ggplot只是繪製你的數據 - 你需要檢查和清理你的數據。看看data.frame中「crop」的不同級別:

> levels(scdata$Crop) 
[1] ""  "alfalfa" "corn" "oat"  "soybean" 

這就是爲什麼在圖例中有一個「額外」的空白裁剪。你可以看到相關的線路搭配:

scdata[scdata$Crop=="",] 

Which'll告訴你,你已經在一組彙總/總/空行CSV文件的讀取結束。

我也建議不要直接在ggplot通話設置x軸標籤,特別是如果你有很多很多。如果數據不符合您的期望,它很容易導致錯誤標籤問題,這將很難發現。我不知道你想要什麼領域,而是用之類的東西pastesprintf設置您的標籤提前。