如何绘制变量的分布,包括ggplot2中的总分布?

问题描述 投票:0回答:1

我有一个这样的df:

df <- data.frame(ID = 1:200, 
                M500 = rnorm(200),
                COUNTRY = rep( c("PERU", "MEXICO", "COLOMBIA"),length.out = 200))

我想绘制一张小提琴图表,比较这三个国家和总人口的小提琴情况。换句话说,我需要在同一张图表中放置四把小提琴。我的基本剧情是这样的

ggplot(cl3, aes(x = COUNTRY, y = M500, color = COUNTRY, fill = COUNTRY)) +
  geom_violin(alpha = 0.5, trim = TRUE) +
  geom_point(position = position_jitter(width = 0.1), alpha = 0.5, size = 2) +
  stat_summary(fun = "mean", geom = "crossbar", color = "black", size = 0.5) +
  scale_color_brewer(palette = "Set1") +
  scale_fill_brewer(palette = "Set1") +
  theme_minimal() +
  labs(title = "title", x = "COUNTRY", y = "M500")

但是我无法将第四小提琴与总数绘制出来。

ggplot2 rstudio violin-plot
1个回答
0
投票
set.seed(123)
df <- data.frame(ID = 1:200, 
                 M500 = rnorm(200),
                 COUNTRY = rep( c("PERU", "MEXICO", "COLOMBIA"),length.out = 200))

library(dplyr)
library(ggplot2)
library(tidyr)

df %>%
  left_join(df %>% mutate(COUNTRY = 'TOTAL'), by = join_by(ID, M500)) %>%
  pivot_longer(c(COUNTRY.x, COUNTRY.y), values_to = 'COUNTRY') %>%
  ggplot(aes(x = COUNTRY, y = M500, color = COUNTRY, fill = COUNTRY)) +
  geom_violin(alpha = 0.5, trim = TRUE) +
  geom_point(position = position_jitter(width = 0.1), alpha = 0.5, size = 2) +
  stat_summary(fun = "mean", geom = "crossbar", color = "black", linewidth = 0.5) +
  scale_color_brewer(palette = "Set1") +
  scale_fill_brewer(palette = "Set1") +
  theme_minimal() +
  labs(title = "title", x = "COUNTRY", y = "M500")

创建于 2023-12-19,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.