GGRidges:单密度曲线太高

时间:2017-12-21 18:03:50

标签: r ggplot2 scale ggridges

我有一个数据框,它由一个单独的因变量和多个自变量组成。一组关系来自不同的测试(但这对解决我遇到的问题并不重要)。

我正在使用包ggridges来表示每个关系的密度图的一个图。当一个关系只有一个值时,ggridges会产生一个点而不是密度图。我的问题是,在这种情况下,下面的密度图与上面的空间重叠。可能是因为ggridges没有看到另一个密度图并且扩展了下面密度图可以采用的空间。 选项“比例”可用于避免两个密度图的重叠,但不能避免密度图和点之间的重叠(至少我认为是这样)。

如果我设置scale = 0.5,我可以解决问题,但这不是最好的事情,因为每个密度图变得更小。那些不与其他人重叠的那些。

下面我附上了一个可重复的示例,该示例生成了我遇到的问题的图表。 感谢任何人都可以帮助我。

library(magrittr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(forcats)
library(ggplot2)
library(ggridges)
library(RCurl)
#> Carico il pacchetto richiesto: bitops

y_ex <- c("y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1","y1")
x_ex <- c("x1","x1","x2","x3","x3","x3","x3","x3","x3","x4","x4","x4","x5","x5","x5","x5","x5","x5","x5","x5","x5","x5","x5")
value_ex <- c(0.26,0.40,0.47,0.72,0.71,0.69,0.74,0.73,0.24,0.39,0.43,0.46,0.21,0.18,0.14,0.10,0.16,-0.10,-0.11,0.56,0.50,0.49,0.43)

data_ex <- data.frame(y_ex,x_ex,value_ex)

r_ex <- data_ex %>% 
  dplyr::mutate(x_ex = forcats::fct_reorder(x_ex, desc(value_ex), fun = mean))

r_ex %>% 
  ggplot(aes(x = value_ex, y = x_ex)) + 
  ggtitle(paste0("Predictors of ",y_ex)) +
  geom_density_ridges(fill = "royalblue",
                      scale = 0.9,
                      color = NA, 
                      alpha = 0.7,
                      rel_min_height = 0.01) +
  geom_point(size = 0.5, alpha = 0.5, pch = 16) +
  geom_point(data = r_ex %>% group_by(x_ex) %>% dplyr::summarise(value_ex = mean(value_ex)),
             color = "firebrick",
             pch = 16,
             alpha = 0.5) +
  scale_y_discrete("") +
  scale_x_continuous("", limits = c(0, 1)) +
  theme_grey(base_size = 16, base_family = "serif") +
  theme(plot.title = element_text(hjust = 0.5,
                                  lineheight = .8, 
                                  face = "bold",
                                  margin = margin(10, 0, 20, 0),
                                  color = "gray15"),
        legend.position = "none") 
#> Picking joint bandwidth of 0.0453
#> Warning: Removed 2 rows containing non-finite values (stat_density_ridges).
#> Warning: Removed 2 rows containing missing values (geom_point).`

enter image description here

1 个答案:

答案 0 :(得分:1)

发生的事情是缩放启发式被丢失的数据搞糊涂了。缩放启发式获取基线y值的总范围,并将其除以组的数量 - 1,参见here

在您的情况下,缩放启发式产生的参考比例恰好是因子2太大,因此您应该使用scale = 0.45来获得与scale = 0.9相同的效果没有任何缺失的水平。

请注意,所有区域都需要缩放在一起,因为分布下的区域需要具有相同的大小(某些单位为1)。你的x5分布不是那么高,因为它们是双峰的,因此更宽。