我认为问题出在width = 0.1 参数上,例如
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
select(species, island, bill_length_mm) %>%
ggplot(aes(x = island, y = bill_length_mm, fill = species)) +
geom_boxplot(width=.1) +
geom_violin()
如果您使宽度相同,它们会按预期排列:
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
select(species, island, bill_length_mm) %>%
ggplot(aes(x = island, y = bill_length_mm, fill = species)) +
geom_boxplot(width=.2) +
geom_violin(width=.2)
此外,与其使用箱线图和小提琴(两者都说明值的分布),不如绘制单个值和分布,例如
library(tidyverse)
library(palmerpenguins)
library(ggbeeswarm)
penguins %>%
na.omit() %>%
select(species, island, bill_length_mm) %>%
rename(Species = species, Island = island) %>%
ggplot(aes(x = Island, y = bill_length_mm, fill = Species)) +
geom_boxplot(width=.4, outlier.shape = NA,
position = position_dodge2(preserve = "single")) +
geom_quasirandom(aes(colour = Species), groupOnX = TRUE,
width=.2, alpha = 0.5, dodge.width = 0.4) +
theme_bw(base_size = 16) +
ylab("Bill Length (mm)")