1

对此可能有一个明显的解决方案,但我对绘图有点天真。我想手动定义point_shapepoint_fill指示例如性别组。我该怎么做呢?

换句话说,我希望看到例如女性的绿色方块和非女性的蓝色三角形(作为任意示例)。(有评论提到剧情上没有点,但是使用ggridges当前开发版的时候有。)

# Simulate data:
df <- data.frame(female = factor(sample(0:1, size = 500, replace = TRUE)),
                 intervention = factor(sample(0:1, size = 500, replace = TRUE))) %>% 
  dplyr::mutate(value = ifelse(female == "1", runif(n = 500, min = 0, max = 100), 
                               rnorm(n = 500, mean = 50, sd = 20)))

# Draw plot:
df %>% 
  ggplot2::ggplot(aes(y = intervention)) +
  ggridges::geom_density_ridges2(aes(x = value, 
                                     colour = "black", 
                                     fill = female),
                                 scale = .7,
                                 alpha = 0.6, 
                                 size = 0.25,
                                 jittered_points = TRUE, 
                                 point_shape = 21,
                                 point_size = 0.85,
                                 point_fill = "black")

这里

4

2 回答 2

3

我们看不到ggridgesCRAN 上当前可用版本的要点。

您正在使用仅 ggridges在 github 上可用的开发版本中可用的选项:

library(tidyverse)
# Simulate data:
df <- data.frame(female = factor(sample(0:1, size = 500, replace = TRUE)),
                 intervention = factor(sample(0:1, size = 500, replace = TRUE))) %>% 
    dplyr::mutate(value = ifelse(female == "1", runif(n = 500, min = 0, max = 100), 
                                 rnorm(n = 500, mean = 50, sd = 20)))

# devtools::install_github("clauswilke/ggridges")
library(ggridges)

ggplot(df, aes(y = intervention)) +
    geom_density_ridges2(aes(x = value, fill = female),
                         scale = .7,
                         alpha = 0.6, 
                         size = 0.25,
                         jittered_points = TRUE, 
                         point_shape = 21,
                         point_size = 0.85,
                         point_fill = "black") 
#> Picking joint bandwidth of 8.06

似乎不可能将点的美学映射到任何变量。它们需要是固定值。我试图手动指定形状,但它不起作用:

ggplot(df, aes(y = intervention)) +
    geom_density_ridges2(aes(x = value, fill = female),
                                   scale = .7,
                                   alpha = 0.6, 
                                   size = 0.25,
                                   jittered_points = TRUE, 
                                   point_shape = c(21,22)[df$female],
                                   point_size = 0.85,
                                   point_fill = "black") 
#> Picking joint bandwidth of 8.06
#> Error: Aesthetics must be either length 1 or the same as the data (2548): size, scale, alpha, point_shape, point_size, point_fill

我能做的最好的就是这不是很好的情节:

ggplot(df , aes(y = intervention)) +
    geom_density_ridges2(aes(x = value, fill = female),
                         scale = .7,
                         alpha = 0.6, 
                         size = 0.25) +
    geom_point(aes(x = value, shape = female), 
               position = position_jitter(height = 0.2, width = 0))
#> Picking joint bandwidth of 8.06

您可能会在github上发布问题/功能请求

reprex 包(v0.2.0)于 2018 年 4 月 1 日创建。

于 2018-03-31T22:35:09.740 回答
3

包作者在这里。发生的情况如下:在一般情况下,您希望能够独立于线条颜色、大小等设置点颜色、大小等样式。但是,标准 ggplot 无法做到这一点。例如,它只有一种color美学,适用于所有点和线。

为了解决这个问题,我创建了专门适用于点的新美学point_colorpoint_size、等。point_shape您可以像往常一样将数据映射到它们上。但是,ggplot 没有办法为它们创建比例,因此我创建了scale_discrete_manual()(和其他一些比例),您可以使用它们来定义适当的比例。

把所有这些放在一起,你会得到这样的结果:

# Draw plot:
df %>% 
  ggplot2::ggplot(aes(y = intervention)) +
  ggridges::geom_density_ridges2(aes(x = value, 
                                     point_color = female,
                                     point_fill = female,
                                     point_shape = female,
                                     fill = female),
                                 scale = .7,
                                 alpha = 0.6, 
                                 size = 0.25,
                                 jittered_points = TRUE, 
                                 point_size = 0.85) +
  ggplot2::scale_fill_manual(values = c("#A0FFA0", "#A0A0FF")) +
  ggridges::scale_discrete_manual(aesthetics = "point_color", values = c("#00BF00", "#0000BF")) +
  ggridges::scale_discrete_manual(aesthetics = "point_fill", values = c("#80FF80", "#8080FF")) +
  ggridges::scale_discrete_manual(aesthetics = "point_shape", values = c(22, 24))

在此处输入图像描述

于 2018-04-01T20:59:28.910 回答