首页 > 解决方案 > 删除图例中的行

问题描述

如何删除与图例上的蓝点相交的线?我能够通过使用从项目 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")) 

标签: rggplot2legend

解决方案


基于这个答案,您也可以覆盖线型。

从颜色中删除线条并填充图例

所以添加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))))

推荐阅读