可能有一个明显的解决方案,但我对绘图有点天真。我希望手动定义point_shape
和point_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")
答案 0 :(得分:3)
我们无法在CRAN上看到当前可用的ggridges
版本的点。
您正在使用仅在开发版本中可用的选项
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 package(v0.2.0)创建于2018-04-01。
答案 1 :(得分:3)
包裹作者在这里。发生的事情如下:在一般情况下,您希望能够独立于线条颜色,大小等设置点颜色,大小等样式。但是,标准ggplot不能这样做。例如,它只有一个color
美学适用于所有点和线。
要解决此问题,我创建了专门适用于点的新美学point_color
,point_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))