2011-06-07 121 views
6

我想用ggplot創建一個顯示方法比較數據的散點圖。這些地塊應該有原始數據,理想線和錯誤的擬合線。圖例應顯示理想線和擬合線的線型/線寬/線條顏色。ggplot散點圖中的傳奇故事

我可以得到大部分的我想要什麼,但有這些問題與傳說:

  • 的圖例顯示2行的每一行的類型,爲什麼?如何解決?

  • 我寧願在傳說中的矩形沒有粉紅色的背景(如果我沒有指定填充顏色則rectanglebackground成爲默認的灰色,這是我不喜歡任何更好)

示例代碼:

set.seed(603) 
x.raw=rnorm(n=30, mean=50, sd=20) 
y.raw=x.raw+rnorm(n=30, mean=2, sd=2) 
x.raw=round(x.raw, 2); y.raw=round(y.raw, 2) 
df=data.frame(x=x.raw, y=y.raw) 

require(ggplot2, quietly=TRUE) 
theme_set(theme_bw()) 
xy.range=range(df$x, df$y) 

p=ggplot(df, aes(x=x, y=y)) + 
geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
geom_smooth(method=lm, fill="red1", aes(colour="Fitted", linetype="Fitted")) + 
geom_abline(intercept=0, slope=1, aes(colour="Ideal", linetype="Ideal")) + 
scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
scale_linetype_manual(name="Lines", 
         values=c("Ideal"="solid", "Fitted"="twodash")) + 
scale_x_continuous(name="Control", limits=xy.range) + 
scale_y_continuous(name="Evaluation", limits=xy.range) + 
opts(title="Method Comparison") 
p 

我真的很感謝大家花時間回覆。雖然有什麼有效的邏輯,我不會在那裏反覆試驗。我確實改變一下代碼進行最後的:最後

  • 做出geom_point使點不被覆蓋
  • 保持通話不斷擴展,使X和Y軸的限制被迫成爲同
  • 類似的說明,加入aspect.ratio = 1,現在理想線從角變爲45°角人LA克利夫蘭

最終代碼到角:

ggplot(df, aes(x=x, y=y)) + 
    geom_smooth(method=lm, se=FALSE, size=1, aes(colour="Fitted", linetype="Fitted")) + 
    geom_smooth(method=lm, fill="red", colour="red", linetype="twodash", size=1) + 
    geom_line(data = data.frame(x=0, y=0), aes(colour = "Ideal", linetype = "Ideal"), size=1) + 
    #geom_abline(intercept=0, slope=1, aes(colour = "Ideal", linetype = "Ideal"), size=0) + 
    geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid", size=1) + 
    geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
    scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
    scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + 
    scale_x_continuous(name="Control", limits=xy.range) + 
    scale_y_continuous(name="Evaluation", limits=xy.range) + 
    opts(title="Method Comparison", aspect.ratio=1) + 
    theme_bw() 
+0

一號線是順利,另一個是abline。至於如何解決這個問題,我想要做的就是不用任何關於實際情節的傳說。然後僞造一些數據,並使用geom_line將其繪製成圖標。但這只是一個解決方法。 – 2011-06-07 13:53:27

回答

5

正如@Iselzer在評論中指出的那樣,這兩行代碼爲ablinesmooth

爲了得到傳說中的背景有一個白色填充,你要欺騙ggplot如下:

  • 創建映射到顏色
  • 與填充一個geom_smooth層創建第二個,幾乎是相同的,geom_smooth層但此時用白色填充,沒有被映射到一個圖例:

的代碼:

p=ggplot(df, aes(x=x, y=y)) + 
    geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
    geom_smooth(method=lm, fill="white", aes(colour="Fitted", linetype="Fitted")) + 
    geom_smooth(method=lm, fill="red") + 
    geom_abline(intercept=0, slope=1, aes(colour="Ideal", linetype="Ideal")) + 
    scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
    scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + 
    opts(title="Method Comparison") + 
    labs(x="Control", y="Evaluation") + 
    theme_bw() 

還要注意,通過使用labs()創建標籤,您可以簡化代碼。這意味着您不必重新創建秤。

enter image description here

0

這裏是我採取的尤斯andrie的代碼,而這兩條線在圖例

ggplot(df, aes(x=x, y=y)) + 
    geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
    geom_smooth(method=lm, fill="white", aes(colour="Fitted", linetype="Fitted")) + 
    geom_smooth(method=lm, fill="red") + 
    geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid") + 
    geom_line(data = data.frame(x=0, y=0), aes(colour = "Ideal", linetype = "Ideal")) + 
    scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
    scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + 
    opts(title="Method Comparison") + 
    labs(x="Control", y="Evaluation") + 
    theme_bw() 

我希望在傳奇控制ggplot的新版本將避免這類黑客攻擊的更好。

2

其實,還有就是要改變這種不添加時髦的解決辦法的方式:

p + theme(legend.key = element_rect(color=NA, fill="white"))