首先,一些資料類似于我正在使用的資料。
rawdata <- data.frame(Score = rnorm(1000, seq(1, 0, length.out = 10), sd = 1),
Group = rep(LETTERS[1:3], 10000))
rawdata$Score <- ifelse(rawdata$Group == "A", rawdata$Score 2,rawdata$Score)
rawdata$Score <- ifelse(rawdata$Group == "C", rawdata$Score-2,rawdata$Score)
stdev <- c(10.78,10.51,9.42)
col <- c("#004d8d", "#cc2701", "#e5b400")
現在,我的代碼geom_density_ridges帶有分位數線,在這種情況下它們將是白色的。
p <- ggplot(rawdata, aes(x = Score, y = Group))
scale_y_discrete()
geom_rect(inherit.aes = FALSE, mapping = aes(ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)),
fill = "grey", alpha = 0.5)
geom_density_ridges(scale = -0.5, size = 1, alpha=0.5, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.975),
vline_color = "white", aes(fill = Group))
scale_color_manual(values = col)
scale_fill_manual(values = col)
labs(title="Toy Graph", y="Group", x="Value")
coord_flip(xlim = c(-8, 8), ylim = NULL, expand = TRUE, clip = "on")
p
我們獲得了以下情節,它完全符合預期。

現在我想知道是否有辦法只讓這條白色的小分位數線對背景透明。我首先嘗試設定vline_color = "transparent"并保留aes(fill = Group)在選項按順序繪制的邏輯的末尾,geom_density_ridges但它不是對不同深淺的灰色背景透明,而是對密度填充透明(因此分位數線消失),這不是我想要達到的目標。
提前感謝您的想法!
uj5u.com熱心網友回復:
可以使用 修改顏色scales::alpha。這可以傳遞給您的顏色引數。
library(ggridges)
library(ggplot2)
rawdata <- data.frame(Score = rnorm(1000, seq(1, 0, length.out = 10), sd = 1),
Group = rep(LETTERS[1:3], 10000))
rawdata$Score <- ifelse(rawdata$Group == "A", rawdata$Score 2,rawdata$Score)
rawdata$Score <- ifelse(rawdata$Group == "C", rawdata$Score-2,rawdata$Score)
stdev <- c(10.78,10.51,9.42)
col <- c("#004d8d", "#cc2701", "#e5b400")
ggplot(rawdata, aes(x = Score, y = Group))
scale_y_discrete()
geom_rect(inherit.aes = FALSE, mapping = aes(ymin = 0, ymax = Inf, xmin = -0.1 * min(stdev), xmax = 0.1 * max(stdev)),
fill = "grey", alpha = 0.5)
geom_density_ridges(scale = -0.5, size = 1, alpha=0.5, show.legend = FALSE,
quantile_lines = TRUE, quantiles = c(0.025, 0.975),
### The only change is here
vline_color = alpha("white", .5), aes(fill = Group))
scale_color_manual(values = col)
scale_fill_manual(values = col)
labs(title="Toy Graph", y="Group", x="Value")
coord_flip(xlim = c(-8, 8), ylim = NULL, expand = TRUE, clip = "on")
#> Picking joint bandwidth of 0.148
#> Warning: Using the `size` aesthietic with geom_segment was deprecated in ggplot2 3.4.0.
#> ? Please use the `linewidth` aesthetic instead.


注意:我不確定背景顏色是否真的是“grey90”,我已經看過了。theme如果您想要準確的話,您可能想要明確指定它。
uj5u.com熱心網友回復:
如果你想要密度曲線的文字透視部分,你需要自己制作間隙:
library(tidyverse)
rawdata %>%
mutate(GroupNum = as.numeric(as.factor(Group))) %>%
group_by(GroupNum, Group) %>%
summarise(yval = first(GroupNum) - density(Score)$y,
xval = density(Score)$x,
q025 = quantile(Score, 0.025),
q975 = quantile(Score, 0.975)) %>%
mutate(Q = ifelse(xval < q025, 'low', ifelse(xval > q975, 'hi', 'mid'))) %>%
ggplot(aes(xval, yval, group = interaction(Group, Q)))
geom_line(size = 1)
geom_ribbon(aes(ymax = GroupNum, ymin = yval, fill = Group),
color = NA, alpha = 0.5, outline.type = 'full',
data = . %>% filter(abs(q025 - xval) > 0.03 &
abs(q975 - xval) > 0.03))
coord_flip()
scale_fill_manual(values = col)
scale_y_continuous(breaks = 1:3, labels = levels(factor(rawdata$Group)),
name = 'Group')
labs(x = 'Score')

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533434.html
標籤:rggplot2格里奇
