Как добавить geom_segment в geom_density_ridges_gradient? - PullRequest
0 голосов
/ 18 июня 2019

Я хотел бы добавить вертикальные сегменты на график с ребристой линией, гистограммы которого показывают настроенные квантили.

Мне удалось получить вертикальные сегменты, если я отобразил цвет заливки с помощью ..x.. Но я бы хотел показать квантили на графиках плотности. Я написал следующий код:

library(datasets)
library(ggplot2)
data("iris")

iris_lines <- data.frame(Species = c("setosa", "versicolor", "virginica"),
                         x0 = c(5, 5.9, 6.5))

Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, fill=(..quantile..))) +
  geom_density_ridges_gradient(jittered_points = FALSE, calc_ecdf = TRUE, quantile_lines = c(TRUE), quantiles =c(0.1,0.25,0.75,0.9),scale=0.9, color='white')+
  geom_segment(data = iris_lines, aes(x = x0, xend = x0, y = as.numeric(Species), yend = as.numeric(Species) + c(.9,.5,.5)), color = "red") + scale_y_discrete(expand = c(0.01, 0))
Figure1

Код работает, если я отображаю цвет заливки как fill = ..x... Я получаю три вертикальные линии, представляющие среднее значение каждого графика плотности; однако, если я сопоставлю цвет заливки как fill = ..quantile.., я получу следующую ошибку:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 1, 3

1 Ответ

0 голосов
/ 18 июня 2019

Хороший график!

Добавьте inherit.aes = F ко второму геому, чтобы он не пытался сопоставить ваши данные с вычислением заполнения в вызове ggplot(aes().

Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, fill=(..quantile..))) +
      geom_density_ridges_gradient(jittered_points = FALSE, 
                                   calc_ecdf = TRUE, 
                                   quantile_lines = c(TRUE), 
                                   quantiles =c(0.1,0.25,0.75,0.9),
                                   scale=0.9, color='white') +
      geom_segment(data = iris_lines, 
                   aes(x = x0, xend = x0, 
                       y = as.numeric(Species), yend = as.numeric(Species) + c(.9,.5,.5)),
                   color = "red", inherit.aes = F) +   #### HERE ####
      scale_y_discrete(expand = c(0.01, 0))
Figure1

enter image description here


Редактировать:

ОП спросил в комментарии о выборочной маркировке некоторых элементов и добавлении метки для срединной линии.Вот подход, вероятно, не самый содержательный.

Figure1 <- ggplot(iris, aes(x=Sepal.Length, y=Species, 
                            fill = (..quantile..), 
                            color = (..quantile..))) +
  geom_density_ridges_gradient(jittered_points = FALSE, 
                               calc_ecdf = TRUE, 
                               quantile_lines = c(TRUE), 
                               quantiles =c(0.1,0.25,0.75,0.9),
                               scale=0.9, color='white') +
  geom_segment(data = iris_lines, 
               aes(x = x0, xend = x0, fill = "median",
                   y = as.numeric(Species), 
                   yend = as.numeric(Species) + c(.9,.5,.5),
                   color = "median")) +   #### HERE ####
  scale_y_discrete(expand = c(0.01, 0)) +

  scale_color_manual(name = "quantile",
                     limits = c(1:3, "median"),
                     values = alpha("firebrick1", c(0, 0, 0, 1)),
                     labels = c("<10%", "10-25%", "IQR", "median")) +
  scale_fill_manual(name = "quantile",
    limits = c(1:3, "median"),
    values = c("cadetblue", "coral", "orange", "white"), 
    na.value = "gray30",
    labels = c("<10%", "10-25%", "IQR", "median"))
Figure1

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...