2017-08-09 522 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

相关问题