【问题标题】:ggridges not showing all factors in legendggridges 没有显示图例中的所有因素
【发布时间】:2021-03-01 03:14:48
【问题描述】:

我正在尝试创建此数据的 RidgePlot:

long_data <- structure(list(Cell_Type = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 
6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L), .Label = c("CD4..T.cells", "CD4..Tcm", "CD8..Tcm", 
"CD8..Tem", "Class.switched.memory.B.cells", "Monocytes", "MPP", 
"naive.B.cells", "NK.cells", "Plasma.cells"), class = "factor"), 
    Sample = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 
    1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
    4L), .Label = c("Sample1", "Sample2", "Sample3", "Sample4"
    ), class = "factor"), Percentage = c(39, 35.1, 12.7405962, 
    4.995628, 11.4, 10.9, 8.9116355, 10.1955, 4.5, 6.1, 18.2643388, 
    7.005608, 5.9, 6, 4.9746425, 10.288099, 2.8, 2.8, 13.7408777, 
    19.657708, 18.8, 21.4, 5.7131852, 19.657708, 0.6, 0.8, 0.2308895, 
    18.295758, 7, 6.2, 7.210278, 3.383666, 9.9, 9.8, 16.9838566, 
    9.087761, 0.1, 1, 11.2297, 10.511573), outlier = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("no", "yes"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-40L))

列异常值是“是”和“否”两个级别的因子,但是当我创建 ggplot 时,它完全忽略了所有带有“是”的行作为异常值。

这是我得到的 ggplot 示例。 我似乎在这里遗漏了一些东西,因为预期的输出是一个带有“no”和“yes”的图例以及两种不同颜色的点。

这是生成它的代码:

ggplot(long_data, aes(x=Percentage, y=reorder(Cell_Type,Percentage))) + 
  geom_density_ridges(scale=2, alpha=0.2)+
  geom_density_ridges(alpha=0, color=NA, jittered_points=T, point_alpha=1, aes(point_color=outlier)) + 
  xlab("Percentage") + ylab("Cell Type") 

我在这里错过了什么?

【问题讨论】:

    标签: r ggplot2 ggridges


    【解决方案1】:

    虽然没有写,geom_density_ridges 以某种方式假设 point_color 将与填充同步,例如,如果我们使用 iris,你可以看到它变成了你不想要的东西:

    dat = iris
    dat$new = factor(sample(1:2,nrow(iris),replace=TRUE))
    ggplot(dat,aes(x=Sepal.Length, y=Species, fill = Species)) +
    geom_density_ridges(aes(point_color = new), alpha = .2, jittered_points = TRUE)
    

    如果您不需要图例并且只有 2 种颜色,您可以使用 @markhogue 提出的解决方案。

    试试下面的方法:

    ggplot(long_data, aes(x=Percentage, y=reorder(Cell_Type,Percentage))) + 
    geom_density_ridges(scale=2, alpha=0.2)+
    geom_point(aes(col=outlier))
    

    如果要抖动:

    ggplot(long_data, aes(x=Percentage, y=reorder(Cell_Type,Percentage))) + 
    geom_density_ridges(scale=2, alpha=0.2)+
    geom_jitter(aes(col=outlier),position = position_jitter(height = 0.1))
    

    【讨论】:

    • 我喜欢 geom_jitter 解决方案,看起来完全符合我的需要!谢谢 StupidWolf 和 markhogue
    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 2018-12-27
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    相关资源
    最近更新 更多