0

First – full disclosure: I am a coding newbie. I have googled this to death, and if there are posts explaining how to do this, then I either didn’t find them or (more likely) don’t understand them!! I tried to post the images but evidently my reputation isn't ready for that...

I am trying to produce some box plots and want the outlier colors to match the fill color of the box, and I want to be able to choose those colors. So here is my original code:

    PIPO.box = ggplot(data = rim.sbs, aes(x=as.factor(Year), y=PIPO2))+
      geom_boxplot(aes(fill=Mulched)) +
      scale_fill_manual(name = " ", values = c("grey", "white")) + labs(x = "", y = "Ponderosa pine seedlings/ha\n") + theme_bw(base_size = 24) + 
      theme(panel.border = element_blank(), 
      panel.grid.major = element_blank(), panel.grid.minor = element_blank(),  
      axis.line = element_line(colour = "black"),
      axis.text = element_text(size = 18))
    PIPO.box

With much googling I figured out how to make the box and outlier colors consistent within treatment. Its mostly what I want, but not in pastel colors! I also want one the treatments to have a filled box, as in the graph created above. So I kind of want a combination of the two.

    PIPO.box = ggplot(data = rim.sbs, aes(x=as.factor(Year), y=PIPO2))+
      geom_boxplot(aes(colour = Mulched)) +
      scale_fill_manual(name = " ", values = c("grey", "white")) + labs(x = " ", y = "Ponderosa pine seedlings/ha\n") + 
      theme_bw(base_size = 24) + theme(panel.border = element_blank(), 
      panel.grid.major = element_blank(), panel.grid.minor = element_blank(),   
      axis.line = element_line(colour = "black"),
      axis.text = element_text(size = 18))
    PIPO.box

I tried scale_fill_manual which lets me choose the colors but then I don’t get the outliers to match. I did find a crafty workaround on one of these help sites, creating a dataset with the outliers in it and then re-adding them in my chosen colors with geom_points, but that code is written for one factor and I can’t figure out how to shift the points to split across both factors, see below. And again it’s those pesky pastels!

    library(plyr)
    plot_Data <- ddply(rim.sbs, .(Year), mutate, Q1=quantile(PIPO2, 1/4), Q3=quantile(PIPO2, 3/4), IQR=Q3-Q1, upper.limit=Q3+1.5*IQR, lower.limit=Q1-1.5*IQR)

    PIPO.box = ggplot(data = rim.sbs, aes(x=as.factor(Year), y=PIPO2))+
      geom_boxplot(aes(fill=Mulched)) +
      scale_fill_manual(name = " ", values = c("grey", "white")) + labs(x = " ", y = "Ponderosa pine seedlings/ha\n") + theme_bw(base_size = 24) +
      theme(panel.border = element_blank(), 
      panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
      axis.line = element_line(colour = "black"),
      axis.text = element_text(size = 18))
    PIPO.box + geom_point(data=plot_Data[plot_Data$PIPO2 > plot_Data$upper.limit | plot_Data$PIPO2 < plot_Data$lower.limit,], aes(x=factor(Year), y=PIPO2, col=factor(Mulched)))

Any help in creating box plots with fill and outlier colors by factor, and of my own choosing, would be greatly appreciated!!

  • 1
    When asking for help (especially plotting help) you should include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run the code. Also make the code you share as minimal as possible. Are these `theme()` calls essential to your question or are they just adding noise? – MrFlick Sep 27 '16 at 17:16
  • If you map both `fill` and `color` aesthetics as in your last attempt, you'll need to set the color values in both `scale_fill_manual` and `scale_color_manual`. You'll also need to dodge your `geom_point` layer to match the dodging of the `geom_boxplot`. – aosmith Sep 27 '16 at 17:35
  • Yes how to do the "dodging" is what I am asking for help on.... – coding_inept Sep 27 '16 at 21:38
  • You need to tell the plot to dodge the points and by how much via `position = position_dodge(width = .75)`. See, e.g, [this answer](http://stackoverflow.com/a/22918534/2461552) – aosmith Sep 28 '16 at 15:32
  • YAY!!!!!!!!!!! That works!! Thank you so much for the help! aosmith, your patience with newbies is really appreciated. Cheers! – coding_inept Sep 29 '16 at 15:51

0 Answers0