如何在分组密度图上添加中值?

How to add median values on top of a grouped density plot?

如果之前有人问过这个问题,我深表歉意。我正在尝试将中值添加到 grouped 密度图的峰值(下面的示例)。

library(dplyr)
library(forcats)

Catalan_elections %>%
  mutate(YearFct = fct_rev(as.factor(Year))) %>%
  ggplot(aes(y = YearFct)) +
  geom_density_ridges(
    aes(x = Percent, fill = paste(YearFct, Option)), 
    alpha = .8, color = "white", from = 0, to = 100
  ) +
  labs(
    x = "Vote (%)",
    y = "Election Year",
    title = "Indy vs Unionist vote in Catalan elections",
    subtitle = "Analysis unit: municipalities (n = 949)",
    caption = "Marc Belzunces (@marcbeldata) | Source: Idescat"
  ) +
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_fill_cyclical(
    breaks = c("1980 Indy", "1980 Unionist"),
    labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
    values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
    name = "Option", guide = "legend"
  ) +
  coord_cartesian(clip = "off") +
  theme_ridges(grid = FALSE)

编辑:

感谢您更新您的问题;我误解并认为你想突出显示中位数(直截了当),但听起来你实际上想要峰值(更复杂)。我还认为这是你的代码,而不是来自 https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html 的示例,所以我没有意识到 Catalan_elections 数据集是公开可用的(例如来自 ggjoy 包)。

这里有一个更相关的解决方案:

library(tidyverse)
library(palmerpenguins)
library(ggridges)
#install.packages("ggjoy")
library(ggjoy)

Catalan_elections_with_max_density <- Catalan_elections %>%
  group_by(Year, Option) %>%
  na.omit() %>%
  mutate(max_density = max(density(Percent, na.rm = TRUE)$y),
         which_max_density = which.max(density(Percent, na.rm = TRUE)$y)) %>%
  mutate(which_max_x_intercept = density(Percent, na.rm = TRUE)$x[which_max_density])

Catalan_elections_with_max_density %>%
  mutate(YearFct = fct_rev(as.factor(Year))) %>%
  ggplot(aes(y = YearFct)) +
  geom_density_ridges(
    aes(x = Percent, fill = paste(YearFct, Option)), 
    alpha = .8, color = "white", from = 0, to = 100,
  ) +
  geom_segment(aes(x = which_max_x_intercept,
                   xend = which_max_x_intercept,
                   y = as.numeric(YearFct),
                   yend = as.numeric(YearFct) + max_density * 48),
               color = "white", size = 0.75, alpha = 0.1) +
  labs(
    x = "Vote (%)",
    y = "Election Year",
    title = "Indy vs Unionist vote in Catalan elections",
    subtitle = "Analysis unit: municipalities (n = 949)",
    caption = "Marc Belzunces (@marcbeldata) | Source: Idescat"
  ) +
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_fill_cyclical(
    breaks = c("1980 Indy", "1980 Unionist"),
    labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
    values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
    name = "Option", guide = "legend"
  ) +
  coord_cartesian(clip = "off") +
  theme_ridges(grid = FALSE)
#> Picking joint bandwidth of 3.16

reprex package (v2.0.1)

于 2021-12-14 创建

注意。我真的不明白 geom_density_ridges() 中的缩放是如何工作的,所以我使用“max_density * 一个常数”来让它大致正确。根据您的用例,您需要调整常量或计算出峰值密度与图的 y 坐标的关系。

原回答:

我没有你的数据集“Catalan_elections”,所以这是一个使用 palmerpenguins dataset:

的例子
library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %>%
  na.omit() %>%
  mutate(YearFct = fct_rev(as.factor(year))) %>%
  ggplot(aes(x = bill_length_mm, y = YearFct, fill = YearFct)) +
  geom_density_ridges(
    alpha = .8, color = "white", from = 0, to = 100,
    quantile_lines = TRUE, quantiles = 2
  ) +
  labs(
    x = "Vote (%)",
    y = "Election Year",
    title = "Indy vs Unionist vote in Catalan elections",
    subtitle = "Analysis unit: municipalities (n = 949)",
    caption = "Marc Belzunces (@marcbeldata) | Source: Idescat"
  ) +
  scale_y_discrete(expand = c(0, 0)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_fill_cyclical(
    breaks = c("1980 Indy", "1980 Unionist"),
    labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
    values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
    name = "Option", guide = "legend"
  ) +
  coord_cartesian(clip = "off") +
  theme_ridges(grid = FALSE)
#> Picking joint bandwidth of 1.92

reprex package (v2.0.1)

于 2021-12-13 创建