【发布时间】:2017-06-21 14:15:36
【问题描述】:
我正在尝试为两个数据系列创建两个单独的图例。基本上我有一个数据系列和回归线,我想要一个带有回归数据的图例,最好是报告的 adjr2,以及另一个带有数据集中点的图例。我是 ggplot2 的新手,我一直无法弄清楚指南(guide_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)
我可以用基本包制作这个情节,但我希望能够有一个用于多个数据集的基本脚本,我认为 ggplot2 更有利于这一点。
编辑:这应该是可重现的:
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
【问题讨论】:
-
提供一个带有一些内置数据集的可重现示例。 stackoverflow.com/questions/5963269/…