ggplot2:如何为添加到散点图的线添加图例?

时间:2017-08-09 14:11:26

标签: r plot ggplot2 legend

我想在ggplot2散点图上比较一些x和y数据。 我想添加一个统一线(y = x),两个(y = 2x),一半(y = x / 2)和更平滑的线来帮助理解数据,但我找不到如何将这些线添加到情节的传奇。有什么想法吗?

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

我希望标签“统一行”,“双重”,“半年”和“更顺畅”等标签。出现在&#39; high&#39;之下而且&#39;低&#39;图例中的标签。

按照User3640617的回答,我尝试了以下代码,但结果仍不令人满意,因为数据点现在在图例中有一个线型和平滑线型链接。

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似乎在color之后添加linetypeaes时感到困扰。将行添加到图例时,类别的顺序似乎会发生变化。

3 个答案:

答案 0 :(得分:5)

您可以将字符值映射到未使用的美学,以欺骗ggplot为您制作图例:

$db = new mysqli('localhost', 'root', '', 'db');
$result = $db->query('SELECT * FROM table');
$data = $result->fetch_all(MYSQLI_ASSOC);
$json = json_encode($data, JSON_PRETTY_PRINT);
print_r($json);

enter image description here

使用ggplot(data=d) + geom_point(aes(x, y, colour=cat)) + geom_abline(aes(intercept=0, slope=1, lty='unity line'), col = "red") 删除+ scale_linetype(name = NULL)图例标题。

答案 1 :(得分:2)

我想没有办法让第二个数据集包含你的行。

获取线条的第二个图例的一种方法是按线型映射线条并硬编码颜色(然后手动调整图例中的颜色)。

这可以这样做:

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'

答案 2 :(得分: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指定线条的颜色和点数。