简体   繁体   中英

How do I keep R from showing a dotted line in the legend when using partial transparency (alpha)?

I am making a plot where some of the data is approximate, so I want to show it with a dotted line. I also want to make some of the lines partially transparent. The problem is, I can't seem to get the legends to not show the dashed line on top of the regular line. I just want it to show the regular line. Here's a sample:

data <- data.frame(types = rep(c("Hi", "Lo", "Left", "Right"), each = 20), year = rep(2000:2019, 4), 
                   values = c(0:19 * 1, 0:19 * 1.33, 0:19 * 1.5, 0:19 * 2))

data$v2[data$year <= 2010 & data$types == "Lo"] <- data$values[data$year <= 2010 & data$types == "Lo"]
data$values[data$year < 2010 & data$types == "Lo"] <- NA



ggplot(data = data) +
  theme(legend.position = "top") +
  geom_line(aes(x = year, y = values, color = types, alpha = types), size = 2) +
  geom_line(aes(x = year, y = v2, color = types, alpha = types), linetype = "dotted", size = 2) + 
  scale_alpha_manual(values = c(1, .5, .5, .5))

If you run it from RStudio, it will not show anything on the legend for Left, Lo or Right. If you create a PDF, you can see a transparent line with dots on top of it. I just want it to show the transparent line and no dots.

How do I do that?

The show.legend = FALSE worked. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM