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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the advice 42- the first answer looks like it is what I need
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
Wouldn't make much sense to use a boxplot if there is only one value per combination of factors.
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
Perhaps: bp <- ggplot(data, aes(x = group, y = days, group = interaction(month, depth) , colour = as.factor(depth) )) + geom_boxplot() ; bp
|
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

2 Comments

On my interactive graphics device this looks a lot better if you insert an "\n", after the month argument to paste,
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.