0

My current legend displays the shapes of the points in the chart, crossed out by the line. Id like to remove this line in the legend and just display the shapes.

The code looks like this:

p <- ggplot(data=cumdf, aes(x=quarters2)) +
  geom_line(aes(y = mean_cumsum, colour='Platform participants'), size = 1.5)+
  geom_point(aes(y = mean_cumsum, colour='Platform participants', shape='Platform participants'), size=3) +
  geom_line(aes(y = mean_interventions, colour='Actions'), size=1.5) +
  geom_point(aes(y = mean_interventions, colour='Actions', shape='Actions'), size=3) +
  geom_line(aes(y = mean_sales, colour="Adopters"), size=1.5) +
  geom_point(aes(y = mean_sales, colour='Adopters', shape='Adopters'), size=3) +
  xlab("Quarters") +
  ylab("Cumulative occurences") +
  scale_shape_manual("", values=c("Platform participants" = 16, "Actions" = 17, "Adopters"=15)) +
  scale_colour_manual("",breaks = c("Platform participants", "Actions", "Adopters"),
                      values = c ("#C80000", "#696969", "#4E33FF")) +
  theme_stata(base_size = 15, base_family = "sans", scheme = "s2color") +
  scale_x_continuous(n.breaks=14) +
  geom_vline(xintercept=3, linetype='dashed', size=1.7)
p

Plot output

TvCasteren
  • 449
  • 3
  • 18
  • 1
    Try with adding `show.legend=FALSE` to your `geom_line`s, i.e. `geom_line(..., show.legend = FALSE)` – stefan Feb 15 '23 at 14:12
  • 1
    In that case I would suggest to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Feb 15 '23 at 14:22
  • 2
    I didn't read the comments before posting the answer (which implemented stefan's recommendation). The code works using _my_ data. Since it doesn't work with yours, it behooves you to make a sample of your data available, and/or clearly show where it doesn't work with your code. FYI, you'd need to do it with all `geom_line` calls, not just one, in case that's what you tried.) – r2evans Feb 15 '23 at 14:36
  • 1
    I re-did it again but now for all lines, and it worked, thanks! I will update the post. – TvCasteren Feb 15 '23 at 14:38
  • 1
    Further, your use of multiple `geom_line`/`geom_point` calls just because you add different `y=` mappings suggest that you can reduce your code by reshaping it (from wide-to-long, see https://stackoverflow.com/q/2185252/3358272, https://stackoverflow.com/q/68058000/3358272) and do just one call to each line/point. I'm happy to demonstrate this if you post sample data. – r2evans Feb 15 '23 at 14:38

2 Answers2

1

Add show.legend to your geom_line. Since you have multiple calls to geom_line, you need to add it to all of them.

I'll demonstrate using mtcars, updated for a factor.

dat <- transform(mtcars, cyl = factor(cyl))

Before the change:

ggplot(dat, aes(mpg, disp, group = cyl, color = cyl, shape = cyl)) + 
  geom_line() + 
  geom_point()

ggplot2, legend has both lines and shapes

Add show.legend=FALSE:

ggplot(dat, aes(mpg, disp, group = cyl, color = cyl, shape = cyl)) +
  geom_line(show.legend = FALSE) +
  geom_point()

ggplot2, legend only shows shapes

r2evans
  • 141,215
  • 6
  • 77
  • 149
0

The answer was found in the comments, by adding show.legend=FALSE to geom.line() .

TvCasteren
  • 449
  • 3
  • 18