我有一个数据框,它由一个因变量和多个自变量组成。关系组取自不同的测试(但这对于解决我遇到的问题并不重要)。
我正在使用包 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).`