首页 > 解决方案 > 在 stat_density_ridges 上覆盖 gom_rect 或 geom_tile

问题描述

我使用 stat_density_ridges 制作了一个山脊线图,我想在顶部覆盖一个矩形。我曾尝试使用 geom_rect 和 geom_tile 来做到这一点,但我无法让它工作。这是我当前的代码。

rm(list=ls(all=TRUE)); cat('\014') # clear workspace
library(tidyverse)
library(ggplot2)
library(viridis) 
library(rstatix)

data(iris)
iris$treatment <- rep(c("A","B","C"), length(iris$Species)/3)
mydf <- gather(iris,Variable,value,Sepal.Length:Petal.Width)

ggplot()+
  geom_tile(aes(x=1, 
                y = 1, width =  1, height = 10), fill = "light blue", alpha = 1)+
  stat_density_ridges(data= mydf, aes(x= value, y = Species,  fill = factor(stat(quantile))),
                      geom = "density_ridges_gradient",
                      calc_ecdf = TRUE,
                      quantiles = c(0.1, 0.9), 
                      scale = 1) +
  scale_fill_manual(
    name = "Probability", values = c("#FF0000A0", "light grey", "#0000FFA0"),
    labels = c("(0, 0.1]", "(0.1, 0.9]", "(0.9, 1]"))+
  coord_cartesian(xlim=c(-10, 10))+
  theme_bw()+
  facet_wrap(~Variable, ncol = 1)

包含 geom_tile 线后,它会返回此错误:Error: Discrete value supplied to continuous scale 如何将显示不同模型置信区间的矩形添加到图中?

删除该行后,我得到以下图像。 删除那条线后,我得到了附加的图像。

标签: rggplot2ridgeline-plot

解决方案


推荐阅读