1

I am running the following line of code

 bp <- ggplot(data, aes(x = Month, y = days, group = Month)) + geom_boxplot()+facet_wrap(~Depth)

which works fine and produces a plot of 3 graphs of 3 different Depths each containing a boxplot for each month (I can't post an img of it yet)

However, I would like to display them on a single graph with Depth colour coded for the 3 variables (25, 100, and 300). How would I achieve this? my data look like this

 depth month days
  25     1    49
  25     1    51
  100    1    52
  100    1    55 
  300    1    52
  300    1    50
  25     2    47
  25     2    48
  100    2    53
  100    2    57
  300    2    56
  300    2    59
  ...   ...  ... 

If this is a duplicate question I apologise but the questions I looked at didn't seem to fit my needs I have tried using

bp + geom_boxplot(position = position_dodge(width = 0.8)) 

as suggested here but didn't create one graph

thanks

Community
  • 1
  • 1
Vaughn
  • 25
  • 5

2 Answers2

1

I'm not sure what it means to ask for "Depth colour coded" so let's start with the request to have them "all on one plot". You can use the interaction function to construct a two-way grouping that will get honored by geom_boxplot:

 bp <- ggplot(dat, aes(x = interaction(month,depth, sep=" x "), y = days)) + 
                   geom_boxplot()
 bp

enter image description here

This might have been what was requested:

 bp <- ggplot(data, aes(x = group, y = days, 
                        group = interaction(month, depth) , colour = as.factor(depth) )) + 
          geom_boxplot() 
 bp

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for the advice 42- the first answer looks like it is what I need – Vaughn Jun 26 '16 at 21:00
  • There is actually an error in the data supplied it should have only 25, 100 and 300 for each month rather than multiples of the same depth – Vaughn Jun 26 '16 at 22:52
  • Wouldn't make much sense to use a boxplot if there is only one value per combination of factors. – IRTFM Jun 26 '16 at 23:02
  • here is what I was trying to achieve `ggplot(data, aes(x = factor(Month), y = days, colour = Depth)) + geom_boxplot(aes(group = group))`. My description probably didn't explain it clearly – Vaughn Jun 27 '16 at 02:10
  • Perhaps: `bp <- ggplot(data, aes(x = group, y = days, group = interaction(month, depth) , colour = as.factor(depth) )) + geom_boxplot() ; bp` – IRTFM Jun 27 '16 at 03:28
  • Yes that works, although I changed it slightly `ggplot(data, aes(x = factor(month), y = days, group = interaction(month, depth) , colour = as.factor(depth) )) + geom_boxplot()` to group them together by month. Thanks for your help – Vaughn Jun 27 '16 at 03:52
1

If I understand your question correctly, your task can be done by following codes,

data <- read.table(text = "depth month days
25     1    49
25     1    51
100    1    52
100    1    55 
300    1    52
300    1    50
25     2    47
25     2    48
100    2    53
100    2    57
300    2    56
300    2    59", header = TRUE)

First, create a new variable group

data$group <- with(data, paste("Month:", month, ",depth:", formatC(depth, width = 3, flag = 0), sep = ""))

Then draw the boxplot, and you have to specify the colour using scale_colour_manual().

bp <- ggplot(data, aes(x = group, y = days, group = group, colour = group)) + geom_boxplot() + scale_colour_manual(values = rep(1:3, 2))
bp
pe-perry
  • 2,591
  • 2
  • 22
  • 33
  • On my interactive graphics device this looks a lot better if you insert an `"\n",` after the `month` argument to `paste`, – IRTFM Jun 24 '16 at 15:28
  • 1
    Great thanks! this looks like it may have solved my problem, although I haven't been able to get it to work with my full dataset just yet. I think I should be able to get it to work with a little bit of playing around – Vaughn Jun 26 '16 at 20:59