【问题标题】:GGplot with multiple/conditional scale_fill_manual for stat_density_ridges用于 stat_density_ridges 的具有多个/条件 scale_fill_manual 的 GGplot
【发布时间】:2020-10-21 23:53:31
【问题描述】:

这是 ggplot 专家的问题...

我的数据集有 4 个变量的 432000 个观察值(一个是数字,其他是因子)。 Predictors 有 6 个级别,Estimate 有 4 个级别,Model 有 2 个级别。 Value 的最大值为 2.6,最小值为 -3。 (我希望您可以使用该信息创建数据。)

情节设置为 4x6 多面情节here's is a 2x3 example of the plot

  • 每一行是一个因子的不同级别 (Predictors)
  • 每一列代表另一个因素的不同水平 (Estimate)
  • 每个迷你图中有两个分布,这是另一个因素 (Model)

目标是绘制:

  • 每列中不同颜色(蓝色、绿色、红色、黄色)的分布(根据Estimate
  • 在每个小图中,该颜色的明暗/色调应该不同(例如,在绿色列中,根据Model 重复颜色顺序)
  • 在每个小图的每个分布上填充两个分位数的尾部(如图中的尾线所示;从每条线到尾部的尾部用黑色/灰色着色)。整个情节中的尾巴可以是相同的。

这是我正在使用的代码示例。它不会以单独的颜色绘制分位数:

pp <- ggplot(dd, aes(x=Value, y=as.factor(Model), fill=factor(Model))) + 
stat_density_ridges(quantile_lines = TRUE, quantiles = c(0.05, 0.95), alpha = 0.95,vline_size = 0.5)+
scale_fill_manual(values = c("red", "white")) +
geom_vline(xintercept = 0, linetype="dashed", color = "black", size=0.5) +
  facet_grid(Predictors~Estimate, scales = "free") + labs(x="Parameter value", y=" ") +
   theme(text = element_text(size = 16)) + theme(axis.title=element_text(face="bold"),  strip.text = element_text(
        size = 16)) + theme(legend.position = "none") 

要为分位数着色,您可以将fill=factor(Model)fill=factor(..quantile..) 交换,但到目前为止,在同一个图中获得两个“填充”是不可能的。在许多其他事情中,我尝试在“填充”中输入多个因素,例如:fill=c(factor(Model), factor(Estimate), ..quantile..) ,但没有奏效。

有什么想法吗?

【问题讨论】:

  • 你可能想看看 ggnewscale 包
  • 感谢您的提示!我会调查的。

标签: r ggplot2


【解决方案1】:

我认为根据您的描述,您的数据看起来有点像这样(尽管我已将其限制为 6000 行):

set.seed(69)

Value      <- rnorm(6000)
Predictors <- factor(rep(LETTERS[1:6], each = 1000))
Estimate   <- factor(rep(rep(letters[1:4], each = 250), 6))
Model      <- factor(rep(rep(c("Model1", "Model2"), each = 125), 24))

Value <- Value + rep(rnorm(6), each = 1000)
Value <- Value + rep(rep(rnorm(4), each = 250), 6)
Value <- Value + rep(rep(rnorm(2), each = 125), 24)

dd <- data.frame(Value, Predictors, Estimate, Model)

听起来您想做的大部分事情都可以通过创建一个新的因子变量来实现,该变量是其他两个因子的结合:

dd$fill_factor <- as.factor(paste0(Model, Estimate))

这意味着我们应该通过对您的代码进行最少的更改来接近预期的效果:

library(ggplot2)
library(ggridges)

my_colors <- c("#0000FF", "#00FF00", "#FF0000", "#FFFF00")

ggplot(dd, aes(x = Value, y = Model, fill = fill_factor)) + 
  stat_density_ridges(quantile_lines = TRUE, 
                      quantiles = c(0.05, 0.95), 
                      alpha = 0.95,
                      vline_size = 0.5) +
  scale_fill_manual(values = c(gsub("0", "6", my_colors),
                               gsub("F", "A", my_colors))) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black", size = 0.5) +
  facet_grid(Predictors ~ Estimate, scales = "free") + 
  labs(x = "Parameter value", y = " ") +
  theme(text = element_text(size = 16),
        axis.title = element_text(face = "bold"),  
        strip.text = element_text(size = 16),
        legend.position = "none")

【讨论】:

  • 这几乎就是我要找的...唯一缺少的是彩色分位数(灰色)。例如,A-a-model1 应该有灰色的尾巴(分位数)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
相关资源
最近更新 更多