图例 geom_line 和 geom_ribbon
Legend with geom_line and geom_ribbon
我正在创建一个图形,其中有一条线和围绕它的置信带。为此,我在 R 中的 ggplot2
中同时使用 geom_line
和 geom_ribbon
。问题是如何处理图例。在其中添加了斜杠,在谷歌搜索了一段时间后,我明白这是一个常见问题。我发现的大多数解决方案都是针对条形图的(例如 ggplot cookbook)。我也找到了抑制它的解决方案,但这对我没有用。
下面我用三个图来说明这个问题。首先,当我只绘制线条时,它看起来不错。然后,当我添加绘图的功能区部分时,会添加斜线。第三个情节是我希望它看起来的样子(显然禁止斜线)。我该如何实现?
编辑: 明确地说,我想要的是下图中的内容(我使用 MS Paint 修复):
library(ggplot2)
library(gridExtra)
set.seed(1)
y <- sin(seq(1, 2*pi, length.out = 100))
x <- 1:100
plotdata <- data.frame(x=x, y=y, lower = (y+runif(100, -1, -0.5)), upper = (y+runif(100, 0.5, 1)))
h1 <- ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin"))
h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x, colour = "bands"), alpha = 0.3)
h3 <- h2 + scale_colour_manual(name='', values=c("bands" = "grey", "sin" = "blue"))
grid.arrange(h1, h2, h3)
尽量把颜色保持在aes
之外,这样图例上就不会显示了:
h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x), colour = "red", alpha = 0.3)
如果您想在图例上显示颜色,您可能不得不求助于添加一些额外的 geom_line
。不要在 geom_ribbon
中使用颜色,然后添加 upper
和 lower
行。
h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x), alpha = 0.3) +
geom_line(aes(x=x, y = lower, color = "bands")) +
geom_line(aes(x=x, y = upper, color = "bands"))
编辑:您还可以使用 fill
比例尺。
h1 <- ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin", fill="sin"))
h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x, fill="bands"), alpha = 0.3) +
geom_line(aes(x=x, y = lower, color = "bands")) +
geom_line(aes(x=x, y = upper, color = "bands"))
h3 <- h2 + scale_colour_manual(name='', values=c("bands" = "grey", "sin" = "blue")) +
scale_fill_manual(name = '', values=c("bands" = "grey12", "sin" = "grey"))
grid.arrange(h1, h2, h3)
如果您不想要(几乎不可见的)灰线,您也可以在颜色声明中设置 "bands" = "transparent"
。
您可以制作单独的图例 - 一个用于线条的颜色,一个用于色带的填充。然后使用 scale_...
将图例的名称设置为空白。唯一的问题是图例键将被分开。
ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin"))+
geom_ribbon(aes(ymin=lower, ymax=upper, x=x, fill = "band"), alpha = 0.3)+
scale_colour_manual("",values="blue")+
scale_fill_manual("",values="grey12")
我正在创建一个图形,其中有一条线和围绕它的置信带。为此,我在 R 中的 ggplot2
中同时使用 geom_line
和 geom_ribbon
。问题是如何处理图例。在其中添加了斜杠,在谷歌搜索了一段时间后,我明白这是一个常见问题。我发现的大多数解决方案都是针对条形图的(例如 ggplot cookbook)。我也找到了抑制它的解决方案,但这对我没有用。
下面我用三个图来说明这个问题。首先,当我只绘制线条时,它看起来不错。然后,当我添加绘图的功能区部分时,会添加斜线。第三个情节是我希望它看起来的样子(显然禁止斜线)。我该如何实现?
编辑: 明确地说,我想要的是下图中的内容(我使用 MS Paint 修复):
library(ggplot2)
library(gridExtra)
set.seed(1)
y <- sin(seq(1, 2*pi, length.out = 100))
x <- 1:100
plotdata <- data.frame(x=x, y=y, lower = (y+runif(100, -1, -0.5)), upper = (y+runif(100, 0.5, 1)))
h1 <- ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin"))
h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x, colour = "bands"), alpha = 0.3)
h3 <- h2 + scale_colour_manual(name='', values=c("bands" = "grey", "sin" = "blue"))
grid.arrange(h1, h2, h3)
尽量把颜色保持在aes
之外,这样图例上就不会显示了:
h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x), colour = "red", alpha = 0.3)
如果您想在图例上显示颜色,您可能不得不求助于添加一些额外的 geom_line
。不要在 geom_ribbon
中使用颜色,然后添加 upper
和 lower
行。
h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x), alpha = 0.3) +
geom_line(aes(x=x, y = lower, color = "bands")) +
geom_line(aes(x=x, y = upper, color = "bands"))
编辑:您还可以使用 fill
比例尺。
h1 <- ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin", fill="sin"))
h2 <- h1 + geom_ribbon(aes(ymin=lower, ymax=upper, x=x, fill="bands"), alpha = 0.3) +
geom_line(aes(x=x, y = lower, color = "bands")) +
geom_line(aes(x=x, y = upper, color = "bands"))
h3 <- h2 + scale_colour_manual(name='', values=c("bands" = "grey", "sin" = "blue")) +
scale_fill_manual(name = '', values=c("bands" = "grey12", "sin" = "grey"))
grid.arrange(h1, h2, h3)
如果您不想要(几乎不可见的)灰线,您也可以在颜色声明中设置 "bands" = "transparent"
。
您可以制作单独的图例 - 一个用于线条的颜色,一个用于色带的填充。然后使用 scale_...
将图例的名称设置为空白。唯一的问题是图例键将被分开。
ggplot(plotdata) + geom_line(aes(y=y, x=x, colour = "sin"))+
geom_ribbon(aes(ymin=lower, ymax=upper, x=x, fill = "band"), alpha = 0.3)+
scale_colour_manual("",values="blue")+
scale_fill_manual("",values="grey12")