我想创建一个翻转的小提琴图(在 geom_point() 中为组平均值添加额外点,在 geom_segment() 中添加从最小值到最大值的线)。我使用facet_grid()将这些组集成为facet。 我唯一无法编辑的是每个方面的小提琴图的宽度。所有组都具有相同数量的值,并且应在小提琴图中以相同的小提琴图面积表示。当不使用构面时,我在绘制点和线段时遇到麻烦。 您知道如何实现这一目标吗?
输入数据:
dat1 <- data.frame(subgroup = c(rep(("mountain"), 150), rep(("upland"), 150), rep(("riparian"), 150)),
group = rep(c("birds"), 450),
value = sample(c(1:100), 450, replace = TRUE))
dat2 <- data.frame(subgroup = c(rep(("mountain"), 150), rep(("upland"), 150), rep(("riparian"), 150)),
group = rep(c("beetles"), 450),
value = sample(c(1:50), 450, replace = TRUE))
plot_data <- rbind(dat1, dat2)
plot_data$subgroup <- factor(plot_data$subgroup, levels = c("mountain", "upland","riparian") )
到目前为止我的情节代码:
library(ggplot2)
library(dplyr)
plot_data %>%
ggplot( aes(x = interaction(group, subgroup), y = value, fill = subgroup, label = group)) +
geom_violin(width = 1, alpha = 0.5, scale = "area")+
geom_segment(data = plot_data %>%
dplyr::group_by(group, subgroup) %>%
dplyr::summarise(lower = min(value, na.rm =T),
upper = max(value, na.rm = T)),
aes(x = interaction(group, subgroup) , xend = interaction(group, subgroup),
y = lower, yend = upper), linewidth = 1, col = "grey10", alpha = 0.8)+
geom_point(data = plot_data %>%
dplyr::group_by(group, subgroup) %>%
dplyr::summarise(mean = mean(value, na.rm = T)),
aes(x= interaction(group, subgroup), y = mean), shape = 21, size = 4, col = "black")+
coord_flip()+
labs(y= "Model values", x="")+
facet_grid(group ~ ., scales = "free_y")+
theme_minimal()+
scale_x_discrete(labels= rev(c("riparian", "upland", "mountain")))+
theme(
axis.text.y = element_text(size = 12),
axis.text.x = element_text(size = 10),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 12),
strip.text.y = element_text(size = 14), # Customize facet labels
legend.position = "none")
我遇到的主要问题是小提琴宽度,这里显示正确:
plot_data %>%
ggplot(aes(y = group, x = value, fill = subgroup)) +
geom_violin(width = 1, alpha = 0.5, scale = "area")
但在这里我很难为子组添加点、线和额外标签而不是图例(如上所示)
您的宽度不正确,因为您使用具有自由缩放比例的面。
要重新排序数据,您可以更改
interaction
中变量的顺序。十你不需要使用方面。您可以添加组标签作为文本注释:
plot_data %>%
ggplot( aes(y = interaction(subgroup, group), x = value, fill = subgroup, label = group)) +
geom_violin(width = 1, alpha = 0.5, scale = "area", orientation = "y") +
geom_segment(data = plot_data %>%
dplyr::group_by(group, subgroup) %>%
dplyr::summarise(lower = min(value, na.rm =T),
upper = max(value, na.rm = T)),
aes(y = interaction(subgroup, group) , yend = interaction(subgroup, group),
x = lower, xend = upper), linewidth = 1, col = "grey10", alpha = 0.8) +
geom_point(data = plot_data %>%
dplyr::group_by(subgroup, group) %>%
dplyr::summarise(mean = mean(value, na.rm = T)),
aes(y = interaction(subgroup, group), x = mean), shape = 21, size = 4, col = "black") +
# coord_flip() +
labs(x = "Model values", y = "") +
annotate(geom = "text", label = "birds", x = 100, y = 5, angle = 90, vjust = 1) +
annotate(geom = "text", label = "beetles", x = 100, y = 2, angle = 90, vjust = 1) +
# facet_grid(group ~ ., scales = "free_y")+
theme_minimal()+
scale_y_discrete(labels= rev(rep(c("riparian", "upland", "mountain"), 2)))+
theme(
axis.text.y = element_text(size = 12),
axis.text.x = element_text(size = 10),
axis.title.y = element_text(size = 20),
axis.title.x = element_text(size = 12),
strip.text.y = element_text(size = 14), # Customize facet labels
legend.position = "none")