Search code examples
rggplot2

Geom_violinhalf Plotting Side


I would like to plot half of a violin plot using geom_violinhalf from the package see. I have been able to plot the right half of the violin plot, but I cannot figure out how to plot just the left side of the violin. Here is an example:

install.packages('see')
install.packages('tidyverse')
library(see)
library(tidyverse)

df1 = as.data.frame(matrix(NA, nrow = 10000, ncol = 3))
colnames(df1) = c('d','y','x')
df1[,2] = rnorm(10000,10,5)
df1[,1] = sample(seq(-5,70,.1),10000, replace=TRUE)
df1$x <- floor(df1$d /5) * 5

ggplot(df1, aes(x=x,y=y)) +
  geom_violinhalf(data=df1, aes(group = x), 
                  scale = 'width', fill = alpha('red', 0.2), 
                  color = alpha('red',0.2)) +
  geom_point(aes(x = d), size = 1.2, col = alpha('grey', 0.2)) +
  theme_classic()

Ideally the package would just allow a simple argument i.e. side = 'left' to just plot the violin plot on the left side, but I can't find such an argument within the function. If there's a different package or command I could use instead, I'd be interested in that, but I've had issues with vioplot() so I'd rather not use that.

Also, I have been trying to get the code from

 Plot only one side/half of the violin plot 
to work but it has not been working.

Thanks!


Solution

  • Update - the code in the answer from @PoGibas within this post works perfectly as long as mapping = aes = group(x). This code produces the exact same result as geom_violinhalf, but just on the left side of the violin as opposed to the right.