Jud*_*ude 3 r ggplot2 geom-hline geom-vline
geom_hline()我使用和创建了一个散点图geom_vline(),该图很好,但图例条目不是我想要的显示方式。图例中的(vline恢复)和hline(阈值)是相互交叉的,令人困惑。我希望恢复图例条目为橙色垂直线,阈值图例条目为水平黑线。
我尝试了其他帖子中建议的几件事,但guide_legend(override.aes())要么show.legend = F更改了上面“类型”部分的图例条目(它删除了行并保留彩色圆圈),要么只是删除了其中一行的图例条目。
这是我当前的代码:
\nggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x=Year, y = BRI_adi_moy_transect, shape = River, col = Type)) + \n geom_point(size = 3) + \n geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), width = 0.4) + \n scale_shape_manual(values = c(15, 16, 17)) +\n scale_colour_manual(values = c("chocolate1", "darkcyan")) + \n geom_vline(aes(xintercept = Restauration_year, linetype = "Restoration"), colour = "chocolate1") + \n geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= \'black\') + \n scale_linetype_manual(name = NULL, values = c(4, 5)) + \n scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect),\n max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect))) +\n scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) + \n theme_bw() + \n facet_wrap(vars(River))\nRun Code Online (Sandbox Code Playgroud)\n这是dput我的数据:
structure(list(River = c("Durance", "Durance", "Durance", "Durance", \n"Roya", "Var"), Reach = c("La Brillanne", "Les M\xc3\xa9es", "La Brillanne", \n"Les M\xc3\xa9es", "Basse vall\xc3\xa9e", "Basse vall\xc3\xa9e"), Type = c("restaured", \n"target", "restaured", "target", "witness", "restaured"), Year = c(2017, \n2017, 2012, 2012, 2018, 2011), Restauration_year = c(2013, 2013, \n2013, 2013, 2000, 2009), BRI_adi_moy_transect = c(0.0028, 0.0017, \n0.0033, 0.0018, 0.009, 0.0045), SD_transect = c(0.00128472161839638, \n0.000477209421076879, 0.00204050725984513, 0.000472466654940182, \n0.00780731734792112, 0.00310039904793707)), row.names = c(NA, \n6L), class = "data.frame")\nRun Code Online (Sandbox Code Playgroud)\n知道我怎样才能让它做我想做的事吗?
\n创建两个线型比例。我已将 vline/hline 调用放在底部以获得更好的可见性。
library(tidyverse)
library(ggnewscale)
ggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x=Year, y = BRI_adi_moy_transect, shape = River, col = Type)) +
geom_point(size = 3) +
geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), width = 0.4) +
scale_shape_manual(values = c(15, 16, 17)) +
scale_colour_manual(values = c("chocolate1", "darkcyan")) +
scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect),
max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect))) +
scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) +
theme_bw() +
facet_wrap(vars(River)) +
# here starts the trick
geom_vline(aes(xintercept = Restauration_year, linetype = "Restauration"), colour = "chocolate1") +
scale_linetype_manual(name = NULL, values = 4) +
# ggnewscale is an amazing package
new_scale("linetype") +
# now do the same for geom_hline
geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= 'black') +
scale_linetype_manual(name = NULL, values = 5)
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |