Home > OS >  Formatting a violin plot in ggplot2
Formatting a violin plot in ggplot2

Time:11-12

I'm struggling to format this violin plot how I want it.

enter image description here

Firstly, I was wondering how I could make the outline of the plot, mean and error bars the same colour as the points displayed on each one, while removing the black outline of these points and changing their shape to the same one used for each mean. Secondly I wanted to know how to lower the width of the plot on the x axis and do the same for the error bars so that they don't cover the full width of each violin.

This is the code I currently have:

cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#
Dta_lng %>% 
  ggplot(aes(x= Group, y= `Glucose m 6`, shape=Group))  
  geom_violin()  
  geom_errorbar(stat = "summary", fun.data = "mean_sdl", 
                fun.args = list(mult = 1),
                position =  position_dodge(width = 0.9))  
  geom_jitter(aes(fill=Group),width=0.1, alpha=0.6, pch=21, color="black")  
 #Adjusting limits on Y axis to encompass all data and mimic figure 1a
   ylim(0.0,0.6)  
#Adding colours manually. Original figure is not colour blind friendly so colour blind friendly palette will be used.
  scale_fill_manual(values=cbPalette)   
  theme_classic()  
  # Inserted mean with the corresponding point shapes from original figure. Size versus the other points was increased so the mean is easily identified.
  stat_summary(fun.y=mean, geom="point", shape=c(16,15), size=3)

The data frame looks like this:

Dta_lng

Group Glucose m 6
Efficient 0.4770
Efficient 0.3760
Efficient 0.4960
Efficient 0.3250
Efficient 0.0890
Efficient 0.0460
Efficient 0.2130
Efficient 0.0820
Efficient 0.3590

The inefficient factor is also listed in the left column.

CodePudding user response:

You should always provide some minimal data for us to replicate your results.

For colors the according argument is col = 'color', for the points you should try fill = instead of col = . In the aesthetics try using group = Group instead of shape = Group so that you can specify the shape yourself.

Don't really know about your second question though.

  • Related