0

Is there a reason why ggplot might mess with the geom_point shape in the legend?

In the actual plot everything looks with the shapes correctly plotted as circles, but in the legend it shows them as weird boxes / squares, i.e. it is showing this: enter image description here

But it should show this:

enter image description here

Could it be because I have an ifelse in my geom_point ? This is what I have here for this part: geom_point(aes( y=y, colour=ifelse( (ty>308)&(Time < chron(times=c('08:30:30.0'))), ifelse(side=='left', 'red', 'blue'),'gray')), na.rm = T)

guy
  • 1,021
  • 2
  • 16
  • 40
  • Could you share a reproducible example of your dataset? – www Jun 23 '17 at 14:34
  • How do you add the legend. I think that has something to do with that. – M-- Jun 23 '17 at 14:34
  • @Masoud The legend is automatic here, I did not have to explicitly create it. – guy Jun 23 '17 at 14:39
  • 1
    OK. Include your code and a sample dataset. Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- Jun 23 '17 at 14:40
  • @Masoud I don't get why in R stackoverflow questions, datasets are necessary when in other languages it isn't (e.g. Python). I'm new to R so please feel me in if I am missing something. It seems like no one really knows what's going on until they play around with it themselves. – guy Jun 23 '17 at 14:43
  • 1
    @tbone different classes of data have different behaviors. Sometimes (like [here](https://stackoverflow.com/questions/44701207/column-labels-cropped-when-using-pheatmap)) a text would be treated as integer and it would mess up the plot. This user had not provided a reproducible example but gratefully he found a solution by himself. There are numerous other reasons like easier and more precise reproduction of the error. Addressing the exact problem versus providing a general approach that may or may not resolve OP's problem, etc. Cheers. – M-- Jun 23 '17 at 14:48
  • 1
    https://stackoverflow.com/questions/32646886/r-ggplot2-legend-overlaps-different-point-shapes – M-- Jun 23 '17 at 15:06

1 Answers1

0

This issue is actually because geom_point and geom_line are both plotted and the points are varying according to the size parameter. ggplot is trying to show a point on a line which looks good when it is small and clear but becomes strange and box-like as the size varies.

To make it clearer, turning off the legend for either the line or the points will keep just one.

For example: geom_line(aes(y=foo , colour='green'), show.legend = F)

guy
  • 1,021
  • 2
  • 16
  • 40