使用美学属性在一个ggplot上绘制多个统计函数。

huangapple go评论86阅读模式
英文:

Plot multiple stat functions on one ggplot using aesthetics

问题

我有一个包含分布参数的数据框(这里我使用贝塔分布,但实际上可以是任何分布)。使用beta1beta2作为两个贝塔分布的形状参数的名称(通常称为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功能传递beta1beta2(即shape1shape2)参数。

英文:

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 &lt;- data.frame(dist = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;), 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 &lt;- ggplot(data.frame(x = c(0,1)), aes(x)) +
  stat_function(fun = dbeta,
                colour = &quot;red&quot;,
                args = list(
                  shape1 = 1,
                  shape2 = 3
                )) +
  stat_function(fun = dbeta,
                colour = &quot;blue&quot;,
                args = list(
                  shape1 = 3,
                  shape2 = 3
                )) +
  stat_function(fun = dbeta,
                colour = &quot;green&quot;,
                args = list(
                  shape1 = 3,
                  shape2 = 5
                ))

使用美学属性在一个ggplot上绘制多个统计函数。

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 %&gt;%
  group_by(dist) %&gt;%
  summarise(x = seq(0, 1, 0.01), y = dbeta(x, beta1, beta2)) %&gt;%
  ggplot(aes(x, y, color = dist)) +
  geom_line()

使用美学属性在一个ggplot上绘制多个统计函数。

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 = &#39;Distribution&#39;)

使用美学属性在一个ggplot上绘制多个统计函数。

huangapple
  • 本文由 发表于 2023年7月6日 19:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定