retam611
retam611

Reputation: 23

How to add single values (shapes) to geom_split_violin() based on groups?

I would like to add single reference values to each of my half violins.

enter image description here

It should look something like this with a shape: enter image description here

Here is the code for my current plot (first img):

ggplot(data = csi, aes(x=species,y=CSI, fill=time))+
  geom_split_violin(stat = "ydensity", trim = T,scale = "width")+
  scale_fill_manual(values=c("grey40","grey60"))+
  theme(axis.text.x=element_text(angle=35,hjust = 1))

And this is how my dataset looks like:

str(csi)
'data.frame':   265196 obs. of  3 variables:
 $ species: Factor w/ 17 levels "Tilia europaea",..: 8 8 8 8 8 8 8 8 8 8 ...
 $ time   : Factor w/ 2 levels "present","future": 1 1 1 1 1 1 1 1 1 1 ...
 $ CSI    : num  0.395 0.66 0.615 0.612 0.808 ...

head(csi)
           species    time       CSI
1 Acer platanoides present 0.3953996
2 Acer platanoides present 0.6603609
3 Acer platanoides present 0.6148618
...

I have an additional dataframe with the values per species and time I'd like to add to my plot:

                 species    time      mean
1       Acer platanoides present 0.7069132
2       Acer platanoides  future 0.4984167
3            Acer rubrum present 0.2257700
4            Acer rubrum  future 0.1622086
...

How can I achieve this? Thanks in advance and best regards

Upvotes: 0

Views: 147

Answers (1)

retam611
retam611

Reputation: 23

I found the answer I was looking for here. For geom_point() just the addition position=position_dodge(width=0.7) or another number is needed. Thanks to the people from the link above!

Here is my plot: enter image description here

And the final code: (I merged the two df mentioned in the first post adding another col "mean" to the first df)

  geom_split_violin(aes(x = species,y = CSI, fill = time),
                    stat = "ydensity", trim = T, scale = "width")+
  geom_point(mapping = aes(x = species, y = mean, shape = time), position = position_dodge(width = 0.7))+
  theme(axis.text.x=element_text(angle=35,hjust = 1))

Upvotes: 1

Related Questions