Recently I am doing violin plot combine with box plot in R,
my data is
data = structure(list(Sample.ID = c("S-1", "S-2", "S-3", "S-4", "S-5",
"S-6", "S-7", "S-8", "S-9", "S-10", "S-11", "S-12", "S-13", "S-1",
"S-2", "S-3", "S-4", "S-5", "S-6", "S-7", "S-8", "S-9", "S-10",
"S-11", "S-12", "S-13"), Season = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Summer", "Winter"), class = "factor"),
D.N = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L), .Label = c("Day", "Night"), class = "factor"), Tide.type = structure(c(3L,
1L, 2L, 2L, 1L, 3L, 3L, 3L, 1L, 2L, 2L, 1L, 3L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 1L, 2L, 2L, 1L, 3L, 3L), .Label = c("Flat",
"High", "Low"), class = "factor"), MPs..items.m3. = c(134,
144, 154, 156, 142, 122, 114, 148, 146, 152, 112, 108, 104,
224, 212, 100, 141.3333333, 193.3333333, 157.3333333, 129.3333333,
146.6666667, 188, 93.33333333, 210.6666667, 122.6666667,
114.6666667)), row.names = c(NA, -26L), class = "data.frame")
now when I am using the box plot code combine with violin the box is not align inside the violin plot. how to fix it.
My code is
data$Season = as.factor(data$Season)
data$D.N = as.factor(data$D.N)
data$Tide.type = as.factor(data$Tide.type)
str(data)
library(ggplot2)
ggplot(data, aes(x=Season, y=MPs..items.m3.)) +
geom_violin(aes(fill = D.N),)+
geom_boxplot(aes(fill = D.N), width = 0.2)
My picture look like this
You need to set the correct width of
position_dodge
: