【问题标题】:Remove line in legend删除图例中的行
【发布时间】:2020-05-02 18:13:48
【问题描述】:

如何删除与图例上的蓝点相交的线? 我能够使用

从项目 1 和 2 中删除点
guides(color = guide_legend(override.aes = list(shape = c(NA,NA,16))))

但无法从第三项中删除该行。预先感谢。

下面是我正在使用的代码......

library(ggplot2)
library(scales)
white.st2 <- structure(list(Year = c(1992, 1993, 1994, 1995, 1996, 1997, 1998, 
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
), ws_est_abundance = c(NA, 18257, 144672, NA, NA, 143795, 98717, 
NA, NA, 57641, 32283, NA, NA, 55180, 124844, 175981, 100915, 
90702), perc_ws_pop = c(NA, 3.789, 4.418, NA, NA, 8.129, 9.088, 
NA, NA, 8.898, 8.595, NA, NA, 5.252, 5.599, 6, 6.2, 6.899), est_ws_abundance = c(NA, 
692, 6392, NA, NA, 11689, 8971, NA, NA, 5129, 2775, NA, NA, 2898, 
6991, 10559, 6257, 6258)), row.names = c(NA, -18L), class = "data.frame")

ggplot(white.st2,aes(Year,est_ws_abundance)) + geom_point(aes(color="Estimated abundance\n of 15-year 
old white sturgeon"),size=3) + labs(x="", y="Number of 15-Year Old\n White Sturgeon",title='Estimated 
Abundance of 15-Year old White Sturgeon\n In San Pablo and Suisun Bays, 1992-2009') + theme_bw() + 
geom_hline(aes(yintercept=5500,color='1967-1991 average')) + 
geom_hline(aes(yintercept=11000,color='AFRP production target'),size=0.7) + 
theme(legend.position='bottom',
legend.direction = "horizontal",legend.title = element_blank()) + 
scale_y_continuous(labels=comma,breaks=seq(0,13000,1000))  + scale_x_continuous(breaks=seq(1992, 
2009, 1)) + guides(color = guide_legend(override.aes = list(shape = c(NA,NA,16)))) +
    scale_colour_manual("", breaks = c("1967-1991 average", "AFRP production target","Estimated 
abundance\n of 15-year old white sturgeon"),values = c("green", "red","blue")) 

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    根据这个答案,您也可以覆盖线型。

    Remove lines from color and fill legends

    所以将linetype=c(1,1,NA) 添加到override.aes 列表中。换句话说,改变你的:

    guides(color = guide_legend(override.aes = list(shape = c(NA,NA,16))))
    

    到:

    guides(color = guide_legend(override.aes = list(shape = c(NA,NA,16), linetype=c(1,1,NA))))
    

    【讨论】:

    • 老实说,作为一个更简单的选择,我通常使用注释线和文本而不是 geom_hline,它也可以直接在图表中获取我的线标签......但这只是我的偏好
    • @Michael Tallino,谢谢。我尝试了很多选项,但没有想到线型。我不知道注释行。你有这样的例子吗?将来我会对此感兴趣。
    • @Salvador 不是很方便——但基本上你只是使用 ggplot2 注释来制作一条跨越整个图形的线和另一个文本注释。与此代码类似,但更改 x=0,xend=10,y=2.2 或类似 stackoverflow.com/questions/53158006/…
    猜你喜欢
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 2018-11-03
    • 2022-07-29
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多