0

I created a line plot using ggplot that has 10 lines, and added points (of different shapes) to only two of the lines using subset. However, the legend shows points on all of the lines. Is there a way to make the legend only show a point (of the right shape) on the line that has the points? Here is my code:

aplot <- ggplot(weekly, aes(x=week, y=alpha, group=bin, color=bin))
 +geom_line()

aplot <- aplot +geom_point(aes(x=week, y=alpha, group=bin, 
color=bin), size=3, shape=16, subset(weekly, bin %in% c("b")))

aplot <- aplot +geom_point(aes(x=week, y=alpha, group=bin, color=bin), 
size=3, shape=17, subset(weekly, bin %in% c("t")))

where the data.frame weekly looks something like:

bin  week  alpha
b    1     10
b    2     12
b    3     16
t    1     14
t    2     18
t    3     8
m    1     13
m    2     19
m    3     9
.    .     .
.    .     .
.    .     .

Thanks!

  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 13 '18 at 19:54

1 Answers1

0
aplot <- ggplot() +
         geom_line(data = weekly, aes(x = week, y = alpha, group = bin, color = bin) +
         geom_point(data = subset(weekly, bin %in% c("t")), aes(x = week, y = alpha, group = bin, color = bin), size = 3, shape = 17) +
         geom_point(data = subset(weekly, bin %in% c("b")), aes(x = week, y = alpha, group = bin, color = bin), size = 3, shape = 16) + 
         guides(color = guide_legend(override.aes = list(shape = c(NA, 16, 17))))

Edit: Thanks for including your data sample and for clarifying your question. I tested the code using the guides argument and it now shows different legend shapes

Punintended
  • 727
  • 3
  • 7
  • Thank you for your answer. I'm aware that I can add multiple geom_point() arguments, but the problem is that when I add points like this, the legend now also includes both of these points (so that in this case the legend for all bins includes shapes 16 and 17). I want to make it so that the legend entry for bin "b" only has shape 16, the legend entry for bin "t" only has shape 16, and the legend entry for all other bins does not include any shapes (so that it is just a line). – user7352235 Mar 13 '18 at 20:12