我想显示 1 个结果和 3 个其他变量之间计算的相关系数后验的分位数点图。我希望分位数点图在同一图中分别显示三个相关性的后验分布。在每个分布中,我想根据大小的“经验法则”解释对点进行颜色编码——例如,一些相关性将显示属于小、中或强范围的点的混合。
无论我如何尝试,我都会得到一个图,该图通过我用于颜色代码的第三个变量将我的三个分布分开。
显示问题的代码如下:
library(dplyr)
library(ggplot2)
library(ggdist)
# Example data (replace this with your actual data)
set.seed(123)
data <- data.frame(
variable = rep(c("Variable1", "Variable2", "Variable3"), each = 300),
correlation = c(rnorm(300, 0.2, 0.1), rnorm(300, 0.4, 0.1), rnorm(300, 0.6, 0.1))
)
# Categorizing correlation strength
data <- data %>%
mutate(correlation_strength = case_when(
abs(correlation) < 0.3 ~ "Small",
abs(correlation) >= 0.3 & abs(correlation) < 0.5 ~ "Medium",
abs(correlation) >= 0.5 ~ "Strong"
))
ggplot(data, aes(x = variable, y = correlation, color = correlation_strength)) +
stat_dots(quantiles = 50, position = position_dodge(width = 0.8)) +
scale_color_manual(values = c("Small" = "blue", "Medium" = "green", "Strong" = "red")) +
theme_minimal() +
labs(title = "Posterior Draws of Correlation Coefficients",
subtitle = "Dots colored by correlation strength",
x = "Variable",
y = "Correlation Coefficient",
color = "Correlation Strength")
在ggplot aes参数中,我尝试了传递slab_fill和group = NA的不同组合,但没有成功。
应用统计变换后,您可以使用
ggplot2::after_stat()
设置 color
。
library(dplyr)
library(ggplot2)
library(ggdist)
# Example data (replace this with your actual data)
set.seed(123)
data <- data.frame(
variable = rep(c("Variable1", "Variable2", "Variable3"), each = 300),
correlation = c(rnorm(300, 0.2, 0.1), rnorm(300, 0.4, 0.1), rnorm(300, 0.6, 0.1))
)
ggplot(data, aes(x = variable, y = correlation)) +
stat_dots(
aes(color = after_stat(
case_when(
abs(y) < 0.3 ~ "Small",
abs(y) >= 0.3 & abs(y) < 0.5 ~ "Medium",
abs(y) >= 0.5 ~ "Strong"
)
)),
quantiles = 50, position = position_dodge(width = 0.8)
) +
scale_color_manual(
values = c("Small" = "blue", "Medium" = "green", "Strong" = "red")
) +
theme_minimal() +
labs(
title = "Posterior Draws of Correlation Coefficients",
subtitle = "Dots colored by correlation strength",
x = "Variable",
y = "Correlation Coefficient",
color = "Correlation Strength"
)