jagged contour lines in Violin plots - how to fix this?

This should be a relative easy questions or a question with no answer (I am also new to R).

I am plotting simple violin graphs (with boxplot) following guidelines provided online. It all works fine, but when the violins are plotted their contour line is annoyingly jagged/pixelated and I don't seem to find any ways to adjust this (boxplots look fine). It is not an issue of exporting as the issue is present already in the plots window in Rstudio.

Is this due to the version of Rstudio I am using ( version 3.5.3 (2019-03-11))? is there any function that could help getting a nice smooth contour line.

So my issue is merely aesthetic, but it makes a massive difference when graphs are presented in figures on a manuscript.

Any suggestion truly appreciated thank you

Below the lines of code, FYI

Violin0plot <- ggplot(RtG0plot, aes(variable, value)) + 
  geom_violin(trim = TRUE, fill = "#774474", colour = "black") +
  geom_boxplot(width = 0.1, size = 0.75, position = position_dodge(0.95), fill = "yellow", colour = "black") +
  labs(x= "pattern", y="Error rate") +
  ylim (0,40) + theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
Violin0plot

Please post some data, the code to graph it, and the graph itself, so we can reproduce the problem. Here is a link to a post about Reproducible Examples:

Hi
apologies.
please find here:

df <- data.frame(stringsAsFactors = TRUE,
                 variable = c("Re","C","Ra","T"),
                 value = c(1.666667,	3.333333,	1.666667,	28.333333,
                           3.333333,	0.000000,	0.000000,	16.666667,	
                           5.000000,	5.000000,	3.333333,	28.333333,	
                           5.000000,	2.666667,	3.333333,	18.333333,	
                           8.333333,	1.666667,	0.000000,	13.333333,	
                           8.333333,	1.666667,	0.000000,	23.333333,	
                           1.666667,	6.666667,	1.666667,	31.666667,	
                           3.333333,	0.000000,	1.666667,	5.000000))
 View(df)

Violinplot <- ggplot(df, aes(variable, value)) + 
  geom_violin(trim = FALSE, fill = "#008C96", colour = "black") +
  geom_boxplot(width = 0.1, size = 0.75, position = position_dodge(0.95), fill = "white", colour = "black") +
  labs(x= "pattern", y="Error rate") +
  ylim (0,40) + theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
Violinplot

The violin plot comes out fine. but the outer contour lines are annoyingly pixelled (see image)example%20v%20plot

Thanks very much
Giulia

You could try playing with the bw (bandwidth) argument of geom_violin(). Below are a few versions.

df <- data.frame(stringsAsFactors = TRUE,
                 variable = c("Re","C","Ra","T"),
                 value = c(1.666667,    3.333333,   1.666667,   28.333333,
                           3.333333,    0.000000,   0.000000,   16.666667,  
                           5.000000,    5.000000,   3.333333,   28.333333,  
                           5.000000,    2.666667,   3.333333,   18.333333,  
                           8.333333,    1.666667,   0.000000,   13.333333,  
                           8.333333,    1.666667,   0.000000,   23.333333,  
                           1.666667,    6.666667,   1.666667,   31.666667,  
                           3.333333,    0.000000,   1.666667,   5.000000))

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
ggplot(df, aes(variable, value)) + 
  geom_violin(trim = FALSE, fill = "#008C96", colour = "black") +
  geom_boxplot(width = 0.1, size = 0.75, position = position_dodge(0.95), fill = "white", colour = "black") +
  labs(x= "pattern", y="Error rate", title = "Default Bandwidth") +
  ylim (0,40) + theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
#> Warning: Removed 523 rows containing missing values (geom_violin).


ggplot(df, aes(variable, value)) + 
  geom_violin(trim = FALSE, fill = "#008C96", colour = "black", bw = 1.5) +
  geom_boxplot(width = 0.1, size = 0.75, position = position_dodge(0.95), fill = "white", colour = "black") +
  labs(x= "pattern", y="Error rate", title = "Bandwidth = 1.5") +
  ylim (0,40) + theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
#> Warning: Removed 427 rows containing missing values (geom_violin).


ggplot(df, aes(variable, value)) + 
  geom_violin(trim = FALSE, fill = "#008C96", colour = "black", bw =2) +
  geom_boxplot(width = 0.1, size = 0.75, position = position_dodge(0.95), fill = "white", colour = "black") +
  labs(x= "pattern", y="Error rate", title = "Bandwidth = 2") +
  ylim (0,40) + theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
#> Warning: Removed 498 rows containing missing values (geom_violin).


ggplot(df, aes(variable, value)) + 
  geom_violin(trim = FALSE, fill = "#008C96", colour = "black", bw =4) +
  geom_boxplot(width = 0.1, size = 0.75, position = position_dodge(0.95), fill = "white", colour = "black") +
  labs(x= "pattern", y="Error rate", title = "Bandwidth = 4") +
  ylim (0,40) + theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"))
#> Warning: Removed 706 rows containing missing values (geom_violin).

Created on 2019-10-31 by the reprex package (v0.3.0.9000)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.