简体   繁体   English

向ggplot2图表添加第二个图例 - 数据点和回归线

[英]Adding a second legend to a ggplot2 graph - data points and regression lines

I am trying to have two separate legends for two data series. 我试图为两个数据系列分别创建两个图例。 Basically I have a data series and regression lines, and I would like one legend with the regression data, preferably with the adjr2 reported, and another legend with the points from the dataset. 基本上我有一个数据系列和回归线,我想要一个带回归数据的图例,最好是报告的adjr2,另一个带有数据集点的图例。 I'm new to ggplot2, and I have not been able to figure out guides(guide_legend). 我是ggplot2的新手,我无法弄清楚指南(guide_legend)。

Here's my code. 这是我的代码。 I've managed to get just the points to show on the legend, so I would like to create a second one for the dashed regression lines. 我已经设法获得了传奇中显示的点数,所以我想为虚线回归线创建第二个点。 I have a column for the adjusted r2 values, which I would like to call for the 2nd legend. 我有一个调整后的r2值列,我想称之为第二个传奇。

g <- ggplot(acci_bud, aes(x = Elevation, y = DOY, color = Year)) +
       geom_errorbar(aes(ymin = DOY -se, ymax = DOY +se), width = .1, show.legend = F)
g <- g + geom_point()
g <- g + geom_abline(data = acci_elev_slope, aes(slope = slope_coeff, 
     intercept = slope_int, color = year),linetype = 'dashed', show.legend = F)

情节我已经设法制作,减去第二个传奇

I can make this plot with the base package, but I would like to be able to have a basic script that I use for multiple datasets and I think ggplot2 is more conducive to this. 我可以用基础包创建这个图,但我希望能够有一个基本脚本,我用于多个数据集,我认为ggplot2更有利于此。

基础包图,目标是使用ggplot2类似的东西

EDIT: this should be reproducible: 编辑:这应该是可重现的:

acci_bud <- data.frame(  Elevation = rep((seq(from = 500, to = 1250, by = 50)),7),
                   DOY = sample(75:180, 112, replace = TRUE),
                   Year = rep(2009:2015, each = 16),
                   se = 2)

acci_elev_slope <- data.frame ( year = seq(from = 2009, to = 2015, by = 1),
                   slope_coeff = c(0.05, 0.03, 0.051, 0.030, 0.025, 0.025, 0.034),
                   slope_int = c(75.76, 79.52, 81.80, 92.71, 75.76, 72.07, 90.6),                               
                   adjr2 = c(0.87, 0.79, 0.65, 0.89, 0.20, 0.57, 0.90))


acci_bud$Year <- as.factor(acci_bud$Year)
acci_elev_slope$year <- as.factor(acci_elev_slope$year)

g <- ggplot(acci_bud, aes(x = Elevation, y = DOY, color = Year)) + 
       geom_errorbar(aes(ymin = DOY -se, ymax = DOY +se), width = 0.1, show.legend = F)
g <- g + geom_point()
g <- g + geom_abline(data = acci_elev_slope, 
                     aes(slope = slope_coeff, intercept = slope_int, color = year), 
                     linetype = 'dashed', show.legend = F)
g

You could achieve this by adding a fake fill legend to the points layer and using show.legend = TRUE in geom_abline . 您可以通过向点图层添加假fill图例并在geom_abline使用show.legend = TRUE来实现此geom_abline Then you can jump through hoops to set the title/label of the new legend, setting colors to use in both legends, and then overriding how the legends look via override.aes in guide_legend . 然后你就可以通过篮球来设置新的传奇的标题/标签跳,设置颜色在这两个传说中的使用,然后重写传说如何通过看override.aesguide_legend

ggplot(acci_bud, aes(x = Elevation, y = DOY, color = Year)) + 
    geom_errorbar(aes(ymin = DOY -se, ymax = DOY +se), width = 0.1, show.legend = FALSE) + 
    geom_point(aes(fill = Year)) + 
    geom_abline(data = acci_elev_slope, 
              aes(slope = slope_coeff, intercept = slope_int, 
                  color = year), linetype = "dashed", show.legend = TRUE) +
    scale_fill_discrete(name = "slopes", labels = acci_elev_slope$slope_coeff) +
    scale_color_manual(values = rainbow(length(unique(acci_bud$Year)))) +
    guides(color = guide_legend(override.aes = list(linetype = 0)),
          fill = guide_legend(override.aes = list(shape = NA, 
                                            color = rainbow(length(unique(acci_bud$Year))))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM