如何在同一散点图上使用geom_vline()和geom_hline避免图例中的交叉效果? [英] How to avoid the crossing effect in legend with geom_vline() and geom_hline on the same scatter plot?

查看:76
本文介绍了如何在同一散点图上使用geom_vline()和geom_hline避免图例中的交叉效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 geom_hline() geom_vline()创建了一个散点图,该图很好,但图例条目并不是我希望它们显示的方式. vline (Restauration)和 hline (Threshold)在图例中彼此交叉,令人困惑.我希望恢复图例条目为橙色垂直线,而阈值图例条目为水平黑线.

I created a scatter plot with geom_hline() and geom_vline(), the plot is good but the legend entries are not how I would like to make them appear. The vline (Restauration) and hline (Threshold) are crossing each other in the legend, making it confusing. I want the restauration legend entry to be an orange vertical line and the Threshold legend entry to be a horizontal black line.

我尝试了其他帖子中建议的一些方法,例如 guide_legend(override.aes()) show.legend = F ,但它更改了图例条目类型"上方的部分(删除了这些行并保留了彩色圆圈),或者只是删除了其中一行的图例条目.

I tried several things suggested in other posts, with guide_legend(override.aes()) or with show.legend = F but either it changed the legend entry for the "Type" section just above (it deleted the lines and kept the coloured circles) or it just deleted that legend entry for one of those lines.

这是我当前的代码:

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")) +  
  geom_vline(aes(xintercept = Restauration_year, linetype = "Restoration"), colour = "chocolate1") + 
  geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= 'black') + 
  scale_linetype_manual(name = NULL, values = c(4, 5)) + 
  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))

这是我的数据的 dput :

structure(list(River = c("Durance", "Durance", "Durance", "Durance", 
"Roya", "Var"), Reach = c("La Brillanne", "Les Mées", "La Brillanne", 
"Les Mées", "Basse vallée", "Basse vallée"), Type = c("restaured", 
"target", "restaured", "target", "witness", "restaured"), Year = c(2017, 
2017, 2012, 2012, 2018, 2011), Restauration_year = c(2013, 2013, 
2013, 2013, 2000, 2009), BRI_adi_moy_transect = c(0.0028, 0.0017, 
0.0033, 0.0018, 0.009, 0.0045), SD_transect = c(0.00128472161839638, 
0.000477209421076879, 0.00204050725984513, 0.000472466654940182, 
0.00780731734792112, 0.00310039904793707)), row.names = c(NA, 
6L), class = "data.frame")

有什么主意我可以让它做我想做的事吗?

Any idea how I could make it do what I want?

推荐答案

创建两个线型比例.我将vline/hline调用置于底部,以提高可见性.

Create two Linetype scales. I have put the vline/hline calls to the bottom for better visibility.

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) 
  

这篇关于如何在同一散点图上使用geom_vline()和geom_hline避免图例中的交叉效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆