【问题标题】:Distribution curves in ggplot2 raincloud plot display incorrectlyggplot2 雨云图中的分布曲线显示不正确
【发布时间】:2022-06-10 20:13:16
【问题描述】:

我注意到 R 中 raincloud plot 包的奇怪行为。具体来说,density 曲线在某些(但不是全部)情况下对数据 values 很敏感。似乎由 geom_flat_violin() 制作的分布曲线的形状以某种方式与数据值相关(它不应该在哪里),我找不到如何恢复它们的独立性。我设法找到的唯一线索:曲线是根据数据中的最低值收缩的,尽管收缩会影响出现这些值的整个面板,而不仅仅是包含它们的子组。

下面是一个可重现的示例,以及指向其图像输出的链接以说明我的意思。 只是提前说明:raincloud 包(在This paper 中提供)不在 CRAN afaik 上,所以我直接从作者的github repo 中提取了它。我还尝试了一个alternative source file,它重现了 . ggrdiges::geom_density_ridges() 或 {ggdist} 等其他实现对图形参数(例如平滑)没有相同级别的控制,除非我遗漏了什么。

示例代码:

library(reshape2)
library(ggplot2)
source("https://gist.githubusercontent.com/benmarwick/2a1bb0133ff568cbe28d/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R")

# load data and melt into longform
data(iris)
miris <- melt(iris,id.vars = "Species", measure.vars = colnames(iris)[1:4], variable.name = "measurement")

## 1- plotting as is gives horizontally "squashed" curves in two of four panels
ggplot(miris, aes(x = Species, y = value, fill = Species)) +
    geom_flat_violin(position = position_nudge(x = .15, y = 0)) +
    facet_wrap(~measurement) 

## 2- manipulating the group of smallest values seems to fix the relevant panel (but fixing other groups doesn't fix the problem - I tried that) 
airis <- miris
# get indices of data to manipulate
inds <- intersect(which(airis$Species == "setosa"), which(airis$measurement == "Petal.Width"))
# assign larger values
airis$value[inds] <- rnorm(length(inds), 3, 0.5)

ggplot(airis, aes(x = Species, y = value, fill = Species)) +
    geom_flat_violin(position = position_nudge(x = .15, y = 0)) +
    facet_wrap(~measurement) 

## this second plot shows larger distribution curves for all speceis in the "Petal.Width" panel, although values were only changed for "setosa"

here's a side-by-side image of the data plotted as is, and after adjusting the small values, as done by the above code

有谁知道问题可能出在哪里,或者可以做些什么来解决它?

非常感谢!

【问题讨论】:

  • 您能否更具体地说明“不正确”/“意外”/“以某种方式修改”对您意味着什么?也许展示一张图片来比较你所看到的和你所期望的?
  • 某些面板中的密度曲线比应有的更平坦,所以我假设发生了一些密度值的修改。我已经编辑了这个问题,希望能澄清我的意思。随附的代码包括校正曲线形状之前和之后的图,以显示它们应该是什么样子。这不是一个解决方案,因为它依赖于弥补价值。

标签: r ggplot2 density-plot violin-plot ggridges


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 2023-03-14
相关资源
最近更新 更多