Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Violin Plot in R using ggplot2 on multiple data columns

I am new to R, and trying to make violin plots of species count data for various species at each sampling depth. The data looks like the following

    Depth Cd Cf Cl
1  3.6576  0  2  0
2  4.0000  2 13  0
3  4.2672  0  0  0
4 13.1064  0  2  0
5 14.0000  3 17 10
6 17.0000  0  0  0

With species in columns 2-5 and depth in column one. I am attempting to use ggplot2 in R but assume the data are not organized in a way that can be used by ggplot2. Ideally I would like the depth to be the y-axis and the species along the x-axis, with violin plots for each. Thank you for your help. Alex

like image 798
Alexander Avatar asked May 18 '26 14:05

Alexander


1 Answers

Reshape your data first:

library(tidyverse)

my_dat2 <- my_dat %>% 
  gather(species, val, -Depth) %>% 
  slice(rep(row_number(), val)) %>% 
  select(-val)

ggplot(my_dat2, aes(species, Depth)) +
  geom_violin()

enter image description here

Note that Cl only has a single line because you have only a singly depth.

like image 140
Axeman Avatar answered May 21 '26 05:05

Axeman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!