2017-08-09 194 views
1

我有一些x和y數據,我想在ggplot2散點圖上進行比較。 我想添加一個統一線(y = x),兩個(y = 2x),一半(y = x/2)和更平滑的線條來幫助理解數據,但我找不到如何將這些線添加到情節的傳說。任何想法?ggplot2:如何添加添加到散點圖的線的圖例?

set.seed(123) 
x <- runif(20, 1, 10) 
y <- 0.8 * x + runif(20, -1, 1) 
cat <- factor(c(rep("high", 10), rep("low", 10))) 
d <- data.frame(x, y, cat) 

ggplot(data=d) + 
    geom_point(aes(x, y, colour=cat)) + 
    geom_abline(aes(intercept=0, slope=1), col = "red") + 
    geom_abline(aes(intercept=0, slope=0.5), col="blue", linetype="dotted") + 
    geom_abline(aes(intercept=0, slope=2), col="blue", linetype="dashed")+ 
    geom_smooth(aes(x, y)) 

y vs x scatter plot in ggplot2

我希望標籤的單一線「,「雙重」,「一半」和「平滑」出現以下圖例中的「高」和「低」的標籤。

繼用戶3640617的回答之後,我嘗試了下面的代碼,但結果仍然不令人滿意,因爲數據點現在在圖例中鏈接了一個線型和平滑線型。

ggplot(data=d) + 
    geom_point(aes(x, y, colour=cat)) + 
    geom_abline(aes(intercept=0, slope=1, colour="y = x")) + 
    geom_abline(aes(intercept=0, slope=.5, colour="y = x/2")) + 
    geom_abline(aes(intercept=0, slope=2, colour="y = 2x")) + 
    geom_smooth(aes(x,y, colour="smooth")) + 
    scale_color_manual(
     values=c("red", "darkgreen", "black", "blue", "red", "blue")) + 
    scale_linetype_manual(
     values=c("blank", "blank", "solid", "dashed", "solid", "dotted")) + 
    scale_shape_manual(values=c(1, 1, NA, NA, NA, NA)) 

另外,我似乎不能夠手動更改tinetypes:

scatter plot with ablines and smoother

我知道,我可以簡單地選擇其他顏色,會有更少的混亂,但它應該是可能只有點和線的點的圖例,而不是點和線的點數分別爲,或者不是?

ggplot2似乎被aes之後colorlinetype的加入打擾。將這些行添加到圖例時,類別的順序似乎發生了變化。

回答

0

不是一個非常巧妙的解決辦法也許是,但你可以爲你的單一線創建與信息的附加數據幀如下:

dfabline <- data.frame(intercept=0, slope=1, group="unity line") 

ggplot(data=d) + 
    geom_point(aes(x, y, colour=cat)) + 
    geom_abline(data=dfabline, aes(intercept=intercept, slope=slope, colour=group)) 

您可以指定線的顏色,並使用scale_colour_manual點。

+0

謝謝你爲這個有用的答案!它適用於多行。瀏覽額外的數據框似乎並不是必要的,它對我而言沒有定義額外數據框中的統一線。看起來,ggplot2一旦我嘗試指定線型和/或顏色,就會被打擾,並且它會從圖例中刪除線條。此外,圖例顯示了一些符號,我需要弄清楚如何刪除。 (請參閱下面的答案) – yanfri

5

您可以將字符值只是映射到一個未使用的審美欺騙ggplot製作成一個傳說你:

ggplot(data=d) + 
    geom_point(aes(x, y, colour=cat)) + 
    geom_abline(aes(intercept=0, slope=1, lty='unity line'), col = "red") 

enter image description here

使用+ scale_linetype(name = NULL)刪除linetype圖例標題。

+0

非常感謝Axeman,這對我來說工作得很好! ...除了我添加了多條線,並且圖例中只有統一線被捕獲。對不起,我的不好(我應該提到我有多條線路,我只是認爲我可以推斷解決方案,我會更新我的問題 – yanfri

1

我想沒有辦法讓第二個數據集包含你的行。

獲取線條的第二個圖例的一種方法是按線型映射線條並對顏色進行硬編碼(然後手動調整圖例中的顏色)。

可以這樣做,像這樣:

library(ggplot2) 

# 1 recreate the data 
set.seed(123) 
x <- runif(20, 1, 10) 
y <- 0.8 * x + runif(20, -1, 1) 
cat <- factor(c(rep("high", 10), rep("low", 10))) 
d <- data.frame(x, y, cat) 

# create the lines-data 
d_lines <- data.frame(int = rep(0, 3), 
         sl = c(1, 0.5, 2), 
         col = c("red", "blue", "blue"), 
         lty = c("solid", "dotted", "dashed")) 

# extract the linetypes from the data.frame (with names to map the values correclty) 
ltys <- as.character(d_lines$lty) 
names(ltys) <- as.character(d_lines$lty) 
# extract the colors in the order that ggplot will put the names of the lines (dashed, dotted, solid) 
cols <- as.character(d_lines$col) 
cols <- cols[order(as.character(d_lines$lty))] 

# plot the data 
ggplot() + 
    geom_point(data = d, aes(x, y, colour=cat)) + 
    geom_smooth(data = d, aes(x, y)) + 
    # add the two different line-colors 
    geom_abline(data = d_lines[d_lines$col == "red", ], 
       aes(intercept = int, slope = sl, lty = lty), color = "red") + 
    geom_abline(data = d_lines[d_lines$col == "blue", ], 
       aes(intercept = int, slope = sl, lty = lty), color = "blue") + 
    # change the color of the legend 
    scale_linetype_manual(name = "Linetype", values = ltys, 
         guide = guide_legend(override.aes = list(colour = cols))) 
#> `geom_smooth()` using method = 'loess' 

+0

謝謝大衛這個解決方案!我試圖添加流暢的線路傳奇和我真的很驚訝,它不工作更容易(使用標準繪圖庫似乎更靈活)。 – yanfri

相關問題