0

I am plotting an interaction with ggplot using a script from last year. Last year this worked fine, but now that I installed a new version of ggplot2, it seems to have issues. The first issues was that the classic theme was not able to plot the X and Y-axis. I managed to solved this by adding it to the theme formatting. But now, stat_smooth produces three confidence bands when I have a two-level factor. Not sure why this is happening. This is the code:

   gp <- ggplot(data=myData, aes(x=Sbfld,y=mem,colour=factor(status))) + geom_point(shape=17, size=8, na.rm=TRUE)
   gp <- gp +
   stat_smooth(method="lm", size=2, na.rm=TRUE) +
scale_y_continuous(breaks=seq(-4, max(mem)*1.1, 0.5)) +
theme_classic(base_size=35) +
theme(legend.position="bottom",
    legend.title=element_blank(),
    legend.text=element_text(size=30, face="bold"),
    legend.key.size=unit(2, "cm"), 
    legend.background = element_rect(colour="black"))+
theme(axis.line.x=element_line(colour="black", size=0.5, linetype="solid"),
    axis.line.y=element_line(colour="black", size=0.5, linetype="solid"),
    axis.title.y=element_text(vjust=1.6, size = 40, face="bold"),
    axis.title.x = element_text(vjust=-0.2, size = 40, face="bold"),
    axis.text.x = element_text(size=25,colour="#333333"),
    axis.text.y = element_text(size=25,colour="#333333"),
    panel.grid.minor=element_blank())

Status has two levels: positive and negative and there are about 7 missing values. X and Y are continuous and there are no missing values there.

This is the output:ggplotoutput

Is this a bug in ggplot? Does anybody know how to solve this?

Thanks!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
user6121484
  • 143
  • 2
  • 15
  • Looks like you're getting a smooth line for the missing values as well. Maybe omit them from the stat_smooth layer. – Gregor Thomas May 19 '16 at 17:09
  • I thought that n.rm=TRUE would do a similar thing... I tried to add "data=na.omit(myData)" to the stat_smooth line, but it still keeps giving me the extra confidence band. – user6121484 May 19 '16 at 18:07
  • I'd try something, but you haven't shared any data... perhaps you could [make your example reproducible](http://stackoverflow.com/q/5963269/903061)? – Gregor Thomas May 19 '16 at 18:22
  • The `na.omit` should do it. If you use the dataset without NA values in `ggplot` you'd also get rid of the grey dots. – aosmith May 19 '16 at 20:48
  • Still learning how to make things reproducible... R is kind of new to me. I think it didn't work, because I have three different data frames (one for x, one for y and one for the factor - all of the same length, but only the factor has NAs). So removing the NA from the factor creates data frames of different length. I ended up making all the data frames without the NA cases and that worked indeed! Thanks for all the suggestions! – user6121484 May 21 '16 at 02:09

0 Answers0