【问题标题】:In ggridges, how to colour code points without getting multiple ridges?在ggridges中,如何在没有多个脊的情况下为代码点着色?
【发布时间】:2020-05-12 03:16:24
【问题描述】:

我正在尝试通过索引(在本例中为PASSFAIL)在我的山脊图上显示点。但是当我这样做时,我最终每组有两个山脊。 我想知道如何每组只有一个山脊?

我的代码,以及下面的输出图像:

library(ggplot2)
library(ggridges)
library(scales)
ggplot(dataSet, aes(x = `Vector_fails`, y = Group, fill = Group)) +
  geom_density_ridges(alpha = .1, 
                      point_alpha = 1,
                      jittered_points = TRUE,
                      aes(point_color = PassCode)) +
  labs(title = 'Vector_fails') +
  scale_x_log10(limits = c(0.09, 1000000), 
                breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides="b")

Two ridges per group (but I want one)

【问题讨论】:

标签: r ggplot2 ggridges ridgeline-plot


【解决方案1】:

如果我理解正确,OP 想要为每个Group 绘制一条山脊线,而不管PassCode。另外,这些点应该被绘制出来,但是用PassCode的值着色。

一种可能的解决方案是绘制山脊线没有点,然后添加第二层,其中绘制了点但(然后复制的)山脊线已不可见:

library(ggplot2)
library(ggridges)
library(scales)
ggplot(dataSet, aes(x = `Vector_fails`, y = Group, fill = Group)) +
  geom_density_ridges(alpha = .1) + # plot rigdelines without points
  geom_density_ridges(alpha = 0, # plot points with invisible ridgelines
                      color = NA, 
                      jittered_points = TRUE, 
                      point_alpha = 1, 
                      aes(point_color = PassCode)) +
  labs(title = 'Vector_fails') +
  scale_x_log10(limits = c(0.09, 1000000), 
                breaks = trans_breaks("log10", function(x) 10^x),
                labels = trans_format("log10", math_format(10^.x))) +
  annotation_logticks(sides="b")

数据

由于问题不包括任何可重现的数据,我试图模拟 OP 的dataSet

nr <- 1000L
set.seed(1L)
dataSet <- data.frame(
  Vector_fails = sample(9L, nr, TRUE) * 10^sample(c(0:1, 4:5), nr, TRUE),
  Group = sample(c("Normal", "Trial 1", "Trial 2"), nr, TRUE, prob = c(0.8, 0.1, 0.1)),
  PassCode = sample(c("PASS", "FAIL"), nr, TRUE)
)

【讨论】:

    猜你喜欢
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2019-05-30
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多