本文介绍了传奇与geom_line和geom_ribbon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个图形,在这里我有一条线和信心带。为此,我在 ggplot2 geom_line geom_ribbon 在R中。问题是如何处理这个传说。其中,斜杠被添加,谷歌搜索了很长一段时间后,我明白这是一个常见问题。我发现的大部分解决方案都适用于虱子(如ggplot食谱)。我也找到了解决方案,但这并不适合我。

I am creating a figure where I have a line and confidence bands around it. To do this, I am using both geom_line and geom_ribbon in ggplot2 in R. The problem is how to deal with the legend. In it, slashes are added and after googling for a quite a while I understand that is a common issue. Most of the solutions I have found are for barplots (e.g. the ggplot cookbook). I have also found solutions to suppress it, but that hasn't worked for me.

下面我有三个图表显示了这个问题。首先,当我只画线时,看起来很好。然后,当我添加情节的功能区部分时,会添加斜杠。第三种情节是我希望它看起来(明显地禁止斜杠)。我怎么做到这一点?

Below I have three plots showing the issue. First, when I only plot the line, it looks fine. Then when I add the ribbon part of the plot, the slashes are added. The third plot is how I want it to look (bar the slashes, obviously). How do I achieve this?

编辑:要清楚,我想要的是我在下图中(我使用MS Paint修复的):

To be clear, what I want is what I have in the following figure (which I fixed using 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)


推荐答案

Yo你可以分开图例 - 一个用于线条的颜色,另一个用于填充色带。然后使用比例_... 将图例的名称设置为空白。

You can make to separate legends - one for the color of line and one for the fill of the ribbon. Then with scale_... set the names for the legend to blank. Only problem is that legend keys will be separated.

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")

这篇关于传奇与geom_line和geom_ribbon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:56