5

I am trying to create a grouped violin plot (see figure), where I am plotting 3 levels for four categorical variables. The plot comes out fine given the data other than the fact that the boxes are the same colour as the wider violin plots behind making them difficult to view. Ideally I would like all the boxes to remain white throughout. I understand that the reason the boxes are changing colour is in response to the fill that I have chosen. I am wondering if there is a way to separate the fill for the geom_violin from the geom_boxplot.

Here is the stripped down code I am using

p <- ggplot(df, aes(x=metric, y=value, fill=variable))+
geom_violin(width=0.9, position=position_dodge(0.75), bw=1.5)+
geom_boxplot(width=0.3, outlier.shape = NA, position=position_dodge(0.75))+
scale_fill_manual(values=c("gray50", "gray75", "gray100"),
 breaks=c("res.error.random", "res.error.increase", "res.error.decrease"),
 labels=c("random cost", "overestimated", "underestimated"))

Example of the plot I am creating

joran
175k34 gold badges439 silver badges484 bronze badges
asked Apr 26, 2016 at 5:00
1
  • Delete fill = variable in the ggplot(aes()) and put it in the geom_boxplot(aes()). In this case, only the boxplot will be concerned by the fill argument Commented Apr 26, 2016 at 6:53

1 Answer 1

5

All depends where you write fill :

  • inside ggplot(aes()): all new layers will be concerned.
  • inside geom_boxplot(aes()) : only this layer is concerned.

It is important to write it inside aes, especially if you want to use a scale_fill_manual() later.

Here is a full answer with generated data:

df <- data.frame(var1 = sample(c("A", "B", "C"), 50, replace =T),
 var2 = sample(c("group1", "group2", "group3"), 50, replace =T),
 value = sample(c(1,2,3,4,5,6,7,8,9,10), 50, replace =T))

1.Same color for boxplot and violin [ggplot(aes(fill =))]:

ggplot(df, aes(x=var1, y=value, fill = var2, group = interaction(var1,var2))) +
geom_violin(width=0.9, position=position_dodge(0.75), bw=1.5) +
geom_boxplot(width=0.3, outlier.shape = NA, position=position_dodge(0.75))

enter image description here


2.Different colors [geom_violin(aes(fill =))] :

ggplot(df, aes(x=var1, y=value, group = interaction(var1,var2)))+
geom_violin(width=0.9, position=position_dodge(0.75), bw=1.5, aes(fill = var2))+
geom_boxplot(width=0.3, outlier.shape = NA, position=position_dodge(0.75), fill = "white")

enter image description here

answered Apr 26, 2016 at 11:42
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.