3

I have a plot drawn with the following command:

ggplot(data=test_mod, aes(x=realDist , y=1-value, color=as.factor(foo) , size=as.factor(foo) )) +
  stat_summary(fun.y=mean, geom="line", alpha=0.85 ) + 
  stat_summary(fun.y=mean, geom="point", pch=21, fill="white", size=2 ) +
  #stat_summary(fun.data=mean_cl_normal, geom="errorbar", width=8, size = 0.5) + 
  theme_bw(base_size = 15, base_family = "Palatino") + 
  theme(legend.key = element_blank()) 

And I get this legend:

enter image description here

However, when I uncomment the line 3 of the command:

ggplot(data=test_mod, aes(x=realDist , y=1-value, color=as.factor(foo) , size=as.factor(foo) )) +
  stat_summary(fun.y=mean, geom="line", alpha=0.85 ) + 
  stat_summary(fun.y=mean, geom="point", pch=21, fill="white", size=2 ) +
  stat_summary(fun.data=mean_cl_normal, geom="errorbar", width=8, size = 0.5) + 
  theme_bw(base_size = 15, base_family = "Palatino") + 
  theme(legend.key = element_blank()) 

I get a slightly different legend:

enter image description here

Note the tiny lines which go across the dots after I started using errorbar for my plot.

How can I get rid of those tiny lines appearing in my legend?

cross
  • 1,018
  • 13
  • 32

2 Answers2

6

You can turn off the legend for that element with show_guide=FALSE. show.legend=FALSE.

ggplot(data=test_mod, aes(x=realDist , y=1-value, color=as.factor(foo) , size=as.factor(foo) )) +
  stat_summary(fun.y=mean, geom="line", alpha=0.85 ) + 
  stat_summary(fun.y=mean, geom="point", pch=21, fill="white", size=2 ) +
  stat_summary(fun.data=mean_cl_normal, geom="errorbar", width=8, size = 0.5, show.legend=FALSE) + 
  theme_bw(base_size = 15, base_family = "Palatino") + 
  theme(legend.key = element_blank())
Rorschach
  • 31,301
  • 5
  • 78
  • 129
1

You can simply switch the position of the "errorbar" line of code to be above the "point" line of code. That way, the points will cover the tiny lines instead of the other way around.

ggplot(data=test_mod, aes(x=realDist , y=1-value, color=as.factor(foo) , size=as.factor(foo) )) +
  stat_summary(fun.y=mean, geom="line", alpha=0.85 ) + 
  stat_summary(fun.data=mean_cl_normal, geom="errorbar", width=8, size = 0.5) +
  stat_summary(fun.y=mean, geom="point", pch=21, fill="white", size=2 ) + 
  theme_bw(base_size = 15, base_family = "Palatino") + 
  theme(legend.key = element_blank()) 
Jota
  • 17,281
  • 7
  • 63
  • 93
  • although a viable solution, @nongkrong's answer solves the issue right at its heart. I will have to accept that one, but your answer also gave me an important insight. Didn't know that the order of execution affects the drawings. Thanks! – cross Jul 12 '15 at 19:11
  • @cross Glad it helped you! Just a pointer for asking questions like this: make sure to include a reproducible example in your questions. That way, I can just run the code you provided and get the exact same graph that you have. – Jota Jul 12 '15 at 19:15
  • @user4786271 There are several possible ways. In general, you want to provide a *minimal* reproducible example. You can make one yourself (e.g. `data.frame(x=1:100, y=1:100, z=1:100)`, use one of the built-in datasets (e.g. `mtcars`), or provide the output of `dput(test_mod)` if it isn't too big. Have a read of [how to make an R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more info. – Jota Jul 12 '15 at 19:28