通过变量绘制 ggridges 点形状/颜色?
Plotting ggridges point shapes / colors by variable?
可能有一个明显的解决方案,但我对绘图有点天真。我想手动定义 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")
我们无法看到 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
您可能 post issue/feature 请求 github
由 reprex package (v0.2.0) 创建于 2018-04-01。
包的作者在这里。发生的事情如下:在一般情况下,您希望能够独立于线条颜色、大小等来设置点颜色、大小等的样式。但是,标准 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))
可能有一个明显的解决方案,但我对绘图有点天真。我想手动定义 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")
我们无法看到 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
您可能 post issue/feature 请求 github
由 reprex package (v0.2.0) 创建于 2018-04-01。
包的作者在这里。发生的事情如下:在一般情况下,您希望能够独立于线条颜色、大小等来设置点颜色、大小等的样式。但是,标准 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))