2014-02-11 23 views
0

我將兩個因子映射到ggplot中的color參數,並且想從圖例中刪除一個因子。當指定多個時從ggplot顏色圖例中刪除一個因子

我有這些數據。

Data <- structure(list(StudyArea = c("AAA", "BBB", "CCC", "AAA", "BBB", 
"CCC", "AAA", "BBB", "CCC"), Obs = c(190L, 481L, 219L, 190L, 
481L, 219L, 190L, 481L, 219L), InSituPred = c(180, 462, 199, 
180, 462, 199, 180, 462, 199), InSituSE = c(9.57382456553708, 
16.5306359391421, 9.51070020039693, 9.57382456553708, 16.5306359391421, 
9.51070020039693, 9.57382456553708, 16.5306359391421, 9.51070020039693 
), variable = c("ExSituAAA", "ExSituAAA", "ExSituAAA", "ExSituBBB", 
"ExSituBBB", "ExSituBBB", "ExSituCCC", "ExSituCCC", "ExSituCCC" 
), value = c(NA, 500, 172, 156, NA, 161, 200, 562, NA), SE = c(20.8552264204181, 
16.1382421185167, 21.43693858142, 20.8552264204181, 16.1382421185167, 
21.43693858142, 20.8552264204181, 16.1382421185167, 21.43693858142 
)), .Names = c("StudyArea", "Obs", "InSituPred", "InSituSE", 
"variable", "value", "SE"), row.names = c(NA, -9L), class = "data.frame") 

並使用下面的代碼,可以使下面的情節。

ggplot(Data)+ 
    geom_point(aes(x=StudyArea, y=value, color=variable),size=3, shape=1)+ 
    geom_errorbar(aes(x=StudyArea, ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+ 
    geom_point(aes(x=StudyArea, y=InSituPred, color=StudyArea),size=3, shape=1)+ 
    geom_errorbar(aes(x=StudyArea, ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+ 
    geom_point(aes(x=StudyArea, y=Obs, color=StudyArea),shape="*",size=12) 

Fig

我想從圖例中刪除的StudyArea顏色(AAA:CCC),因此它僅包含ExSituAAA,ExSituBBB,ExSituCCC。

ADDITION 使用下面的代碼(從@shadow有用的評論)我可以創建下圖。

p <- ggplot(Data, aes(x=StudyArea))+ 
    geom_point(aes(y=value, color=variable),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+ 
    geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+ 
    geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12) + 
    scale_color_discrete(breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) 

p + scale_color_manual(name="Study Area \nPrediction", 
         values=c("red", "blue", "darkgreen","red","blue","darkgreen"), 
         breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) 

FigureII

欲一個linetype添加到圖例指定實線=原位和虛線= ExSitu。

我使用lty手動指定linetype(而不是通過因子aes),因爲我還需要指定color。尋找在低於head,數據$ SE是lty = 2,且數據$ InSituSE是lty 1.

>

head(Data) 
    StudyArea Obs InSituPred InSituSE variable value  SE 
1  AAA 190  180 9.573825 ExSituAAA NA 20.85523 
2  BBB 481  462 16.530636 ExSituAAA 500 16.13824 
3  CCC 219  199 9.510700 ExSituAAA 172 21.43694 
4  AAA 190  180 9.573825 ExSituBBB 156 20.85523 
5  BBB 481  462 16.530636 ExSituBBB NA 16.13824 
6  CCC 219  199 9.510700 ExSituBBB 161 21.43694 

因此,關於如何添加linetype圖例與固體任何建議線= InSitu和虛線= ExSitu將不勝感激。

例子:我想補充...提前

figIII

感謝。

+1

不是你的問題的直接答案,也可能是一個品味問題,無論如何,這裏有一個想法:對我來說,將研究區域_both_映射到x和顏色似乎有點多餘。也許一個想法是僅使用顏色來區分In和Ex-Situ? – Henrik

+0

@Henrik,我需要'InSituPred'的'color'和'Obs'匹配'StudyArea.'另外,我想讓'variable'' color'與'StudyArea'相匹配。從@shadow的評論中提供了更多細節和構建的問題,增加了一些補充。感謝您的想法。 –

回答

2

您可以使用?scale_color_discrete指定中斷。在你的情況下,這可能類似於以下內容:

ggplot(Data, aes(x=StudyArea))+ 
    geom_point(aes(y=value, color=variable),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+ 
    geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+ 
    geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12) + 
    scale_color_discrete(breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) 

編輯:是的,可以指定顏色。由於我不太瞭解你想要什麼顏色方案,下面是一些例子(並非所有例子都是完全重要的)。

p <- ggplot(Data, aes(x=StudyArea))+ 
    geom_point(aes(y=value, color=variable),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+ 
    geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+ 
    geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12) 
p + scale_color_manual(name="Study Area \nPrediction", 
         values=c("red", "blue", "darkgreen","red","blue","darkgreen"), 
         breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) 
p + scale_color_manual(name="Study Area \nPrediction", 
         values=c("black", "black", "black", "red","blue","darkgreen"), 
         breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) 
p + scale_color_manual(name="Study Area \nPrediction", 
         values=c("white", "yellow", "pink", "red","blue","darkgreen"), 
         breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) 

附加:如果您在繪圖之前清理並重新構建數據,這將容易得多。這裏是我的attampt:

df <- with(Data, data.frame(area=rep(StudyArea, 2), 
          exarea=c(variable,rep(variable[c(1,4,7)], 3)), 
          value=c(value, InSituPred), 
          se=c(SE, InSituSE), 
          obs = rep(Obs, 2), 
          situ=rep(c("in", "ex"), each=nrow(Data)))) 
df <- df[!duplicated(df),] 

然後繪圖變得更加容易:

p <- ggplot(df, aes(x=area))+ 
    geom_point(aes(y=value, color=exarea),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=value-se, ymax=value+se, color=exarea, lty=situ), cex=0.75)+     
    geom_point(aes(y=obs, color=exarea),shape="*",size=12) 

p + scale_color_manual(name="Study Area \nPrediction", 
        values=c("red", "blue", "darkgreen"), 
        breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) + 
    scale_linetype_manual(name="Situ", 
         values=c(1,2), 
         breaks=c("in", "ex"), 
         labels=c("InSitu", "ExSitu")) 

EDIT2:它可以使用原始數據這一點。您必須將lty放在aes-功能內,然後像以前一樣使用scale_linetype_manual。這裏是:

p <- ggplot(Data, aes(x=StudyArea))+ 
    geom_point(aes(y=value, color=variable),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable, lty="2"), cex=0.75)+ 
    geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+ 
    geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea, lty="1"),cex=0.75)+ 
    geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12) 
p + scale_color_manual(name="Study Area \nPrediction", 
         values=c("red", "blue", "darkgreen","red","blue","darkgreen"), 
         breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) + 
    scale_linetype_manual(name="Situ", 
         values=c(1,2), 
         breaks=c("1", "2"), 
         labels=c("InSitu", "ExSitu")) 

實際上通常更好的做法是重新構造數據。如果你想對這段代碼做更多的修改,這將是非常困難的。代碼已經相當難以閱讀。因此,如果完全有可能重構你的數據集(通常是這樣),那麼就考慮採取上述方法。

+0

@shodow,謝謝你的回答。一旦我使用scale_color_discrete(),是否也可以指定使用的顏色?我想EsSituAAA:ExSituBBB是c(「紅色」,「藍色」,「深綠色」)。但是,僅添加下面的代碼會導致錯誤,因爲有兩種顏色規範。 'scale_color_manual(values = c(「red」,「blue」,「darkgreen」),labs(fill =「StudyArea \ n Prediction」))' –

+0

我已經在上面添加了** ADDITION **,但也可以打開新的問題...感謝您的繼續幫助! –

+0

謝謝!你的重組非常有幫助。然而,對於應用於另一個我擁有的數據集,手動添加'linetype'則有點直接。是否可以像上例那樣手動添加'linetype'? –