英文:
Plot multiple stat functions on one ggplot using aesthetics
问题
我有一个包含分布参数的数据框(这里我使用贝塔分布,但实际上可以是任何分布)。使用beta1
和beta2
作为两个贝塔分布的形状参数的名称(通常称为alpha和beta)。
df <- data.frame(dist = c("a", "b", "c"), beta1 = c(1, 3, 5), beta2 = c(3, 3, 3))
我想要在一个图上绘制这三个分布的概率密度曲线,最好使用ggplot2,并且每个dist
特征都有一个图例。如果我手动操作,可以像这样做:
p <- ggplot(data.frame(x = c(0, 1)), aes(x)) +
stat_function(fun = dbeta,
colour = "red",
args = list(
shape1 = 1,
shape2 = 3
)) +
stat_function(fun = dbeta,
colour = "blue",
args = list(
shape1 = 3,
shape2 = 3
)) +
stat_function(fun = dbeta,
colour = "green",
args = list(
shape1 = 3,
shape2 = 5
))
但理想情况下,我希望使用ggplot的aes
功能传递beta1
和beta2
(即shape1
和shape2
)参数。
英文:
I have a data frame of parameters for distributions (here Im using a beta distribution but it could be any distribution really). Using beta1
and beta2
as the names of the two beta shape parameters (commonly referred to as alpha and beta)
df <- data.frame(dist = c("a", "b", "c"), beta1 = c(1,3,5), beta2 = c(3,3,3))
I would like to plot the pdf curves of all three of these on one graph, preferably with a legend per dist
feature using ggplot2. If I do this manually I can do something like
p <- ggplot(data.frame(x = c(0,1)), aes(x)) +
stat_function(fun = dbeta,
colour = "red",
args = list(
shape1 = 1,
shape2 = 3
)) +
stat_function(fun = dbeta,
colour = "blue",
args = list(
shape1 = 3,
shape2 = 3
)) +
stat_function(fun = dbeta,
colour = "green",
args = list(
shape1 = 3,
shape2 = 5
))
but ideally would pass the beta1 and beta2 (shape1 and shape2) parameters using the ggplot aes
functionality.
答案1
得分: 1
你不能直接将多个参数映射到 stat_function
中使用美学,但这并不意味着如果你已经将它们存储在一个数据框中,就必须手动传递所有参数。
例如,使用简单的 dplyr
汇总,你可以这样做:
library(tidyverse)
df %>%
group_by(dist) %>%
summarise(x = seq(0, 1, 0.01), y = dbeta(x, beta1, beta2)) %>%
ggplot(aes(x, y, color = dist)) +
geom_line()
英文:
You can't use aesthetics to map mutiple parameters directly into stat_function
, but that doesn't mean that you need to pass all the parameters manually if you already have them stored in a data frame.
For example, using simple dplyr
summarising you could do:
library(tidyverse)
df %>%
group_by(dist) %>%
summarise(x = seq(0, 1, 0.01), y = dbeta(x, beta1, beta2)) %>%
ggplot(aes(x, y, color = dist)) +
geom_line()
Or, if you really want to pass the values to stat_function
, then using base R's Map
you could do:
ggplot(df) +
Map(function(dist, beta1, beta2) {
stat_function(fun = dbeta, args = list(shape1 = beta1, shape2 = beta2),
aes(color = {{dist}}))
}, df$dist, df$beta1, df$beta2) +
labs(color = 'Distribution')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论