Reputation: 33
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
Upvotes: 3
Views: 6540
Reputation: 3311
Like you suspected already, you need to reshape your data. Using tidyr::gather
changes the format from "wide" to "long", which is necessary in this case for plotting species on the x-axis. Furthermore, you need to expand your count data which you can achieve with slice
.
library(tidyverse)
zz <- "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"
my_dat <- read.table(text = zz, header = T)
my_dat %>%
gather(species, val, -Depth) %>%
slice(rep(row_number(), val)) %>%
ggplot(aes(species, Depth)) +
geom_violin(adjust = .5)
Upvotes: 2
Reputation: 35227
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()
Note that Cl
only has a single line because you have only a singly depth.
Upvotes: 3