0

How can I remove the line that crosses the blue point on my legend? I was able to remove the dots from items number 1 and 2 by using

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

but can't remove the line from the third item. Thanks beforehand.

enter image description here

Below is the code I am using....

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")) 
markus
  • 25,843
  • 5
  • 39
  • 58
Salvador
  • 1,229
  • 1
  • 11
  • 19

1 Answers1

0

Based on this answer, you can override the linetype also.

Remove lines from color and fill legends

So add linetype=c(1,1,NA) to the override.aes list. In other words change your:

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

to:

guides(color = guide_legend(override.aes = list(shape = c(NA,NA,16), linetype=c(1,1,NA))))
Michael Tallino
  • 821
  • 4
  • 16
  • Honestly, as an easier option I normally use a annotate line and text rather than geom_hline which also gets my line label directly in the graph ... but that's just my preference – Michael Tallino Jan 15 '20 at 23:03
  • @ Michael Tallino, Thank you. I tried many options but didn't think of linetype. I didn't know about annotate line. Do you have an example of that? I will be interested on that for the future. – Salvador Jan 15 '20 at 23:16
  • @Salvador not super handy - but basically you just use the ggplot2 annotate to make a line that spans the whole graph and another annotate for the text. Similar to this code but change x=0,xend=10,y=2.2 or something like that https://stackoverflow.com/questions/53158006/how-to-annotate-the-line-at-ggplot – Michael Tallino Jan 17 '20 at 15:08