0

I am trying to create a figure showing three violinplots on top of each other (on the same x label) in a "pre" condition, and the same for a "post" condition.

With this script:

library(ggplot2)
library(ggbreak)

Elbow=fakebis$Elbow
Wrist=fakebis$Wrist
Tip_of_middle_finger=fakebis$`Tip of middle finger`
Time=fakebis$Time
data<-data.frame(Elbow,Wrist,Tip_of_middle_finger,Time)
positions <- c("Pre", "Post")

p <- ggplot(data, aes(Time, Elbow))+
  theme_classic() +
  xlab("Measurment time relative to tool-use session")+
  scale_x_discrete(limits = positions)+
  scale_y_break(c(4, 27))+
  ylab("Reported position (cm)")
p <- p + geom_violin(aes(colour = "1"), alpha = .5, size=2,trim=FALSE)+
  geom_boxplot(width=0.1)+
  geom_jitter(shape=16, position=position_jitter(0.2))
q <- p + geom_violin(aes(y = Wrist, colour = "2"), alpha = .5, size=2,trim=FALSE)+
  geom_boxplot(width=0.1)+
  geom_jitter(shape=16, position=position_jitter(0.2))
q <- q + geom_violin(aes(y = Tip_of_middle_finger, colour = "3"), alpha = .5, size=2,trim=FALSE)+
  geom_boxplot(width=0.1)+
  geom_jitter(shape=16, position=position_jitter(0.2))
q

I obtain this plot:

enter image description here

I don't understand why the boxplots and the data points are not plotted over the green and blue violins...

Thanks in advance for your help :) !

stefan
  • 90,330
  • 6
  • 25
  • 51
Ishan
  • 1
  • 1
  • 2
    Because you don't map colors within `geom_point` and `geom_boxplot`. – Roland May 31 '23 at 14:19
  • 4
    Well, the issue is most likely that you use the same data (columns) for the three `geom_boxplot` and `geom_jitter` whereas for the violins you use different data columns. Hence, you are plotting three times the same boxplots and points on top of each other. Instead try with e.g. `geom_boxplot(aes(y = Wrist), width=0.1)` for the second boxplot. – stefan May 31 '23 at 14:20
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. As already pointed out the problem is you are changing the `aes()` in `geom_violin` but not `geom_boxplot`. The fact that you are adding layers in chucks really doesn't matter to ggplot. It would probably be better to reshape your data to a longer format for plotting so you can use fewer layers. That would be easier to test with example data. – MrFlick May 31 '23 at 14:27
  • 1
    Please provide enough code so others can better understand or reproduce the problem. – Community May 31 '23 at 17:29

0 Answers0