gpt4 book ai didi

R/ggplot2 : geom_function & facet_wrap with multiple sets of parameters per facet

转载 作者:行者123 更新时间:2023-12-02 01:27:57 24 4
gpt4 key购买 nike

我想使用 ggplot2::geom_function() 来绘制数据框中一组给定参数的函数。然后应通过“年份”参数将它们组织成各个方面。 StackOverflow 上有类似问题的解决方案( herehere ),但我认为我的用例稍微复杂一些。

我的输入是一个数据框。它包含我想要绘制的不同正态分布(mu、sigma、lambda)的参数,以及我想用作facet_wrap 的facets 的centralYear 参数。除了其他用例之外,我还想绘制每个方面的几个正态分布,这些正态分布由另一个参数生成来区分。每年的世代数量可能会有所不同。数据框如下所示:

input data frame

我想要的是如下所示的输出:

output plot

这是一个包含测试数据框的最小工作示例:

# libraries
library(ggplot2) # for plotting
library(dplyr) # for filtering data and pipes


# Input data
fantasy_df <- data.frame(
centralYear = c(2017, 2017, 2017, 2017, 2016, 2016, 2016, 2016, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2013, 2013, 2013),
generation = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3),
mu = c(123.6, 188.7, 234.5, 269.6, 122.6, 188.4, 232.5, 269.6, 117.3, 187.1, 233.2, 271.3, 117.3, 187.3, 232.8, 271.6, 118.4, 193.9, 246.7),
sigma = c(14.6, 14.6, 14.6, 14.6, 14.8, 14.8, 14.8, 14.8, 15.1, 15.1, 15.1, 15.1, 15.4, 15.4, 15.4, 15.4, 17.5, 17.5, 17.5),
lambda = c(0.06, 0.44, 0.34, 0.15, 0.07, 0.46, 0.30, 0.17, 0.07, 0.46, 0.33, 0.15, 0.08, 0.45, 0.33, 0.14, 0.09, 0.53, 0.37)
)

# Plot function & colours
ndist_function <- function(x, mu, sigma, lam) {
lam * dnorm(x, mu, sigma)
}

colours <- c("blue", "red", "green", "violet")


# Simple single plot
singleYear_df <- fantasy_df[1:4, ]

fantasy_df %>%
ggplot() +
geom_function(fun = ndist_function,
args = list(singleYear_df[1, 3], singleYear_df[1, 4], singleYear_df[1, 5]),
colour = colours[1], lwd = 1.5) +
geom_function(fun = ndist_function,
args = list(singleYear_df[2, 3], singleYear_df[2, 4], singleYear_df[2, 5]),
colour = colours[2], lwd = 1.5) +
geom_function(fun = ndist_function,
args = list(singleYear_df[3, 3], singleYear_df[3, 4], singleYear_df[3, 5]),
colour = colours[3], lwd = 1.5) +
geom_function(fun = ndist_function,
args = list(singleYear_df[4, 3], singleYear_df[4, 4], singleYear_df[4, 5]),
colour = colours[4], lwd = 1.5) +
xlim(0, 300)+
ylab("Density")



# Complex wrapped plot
gen1 <- filter(fantasy_df, generation == 1)
gen2 <- filter(fantasy_df, generation == 2)
gen3 <- filter(fantasy_df, generation == 3)
gen4 <- filter(fantasy_df, generation == 4)

ggplot(data=fantasy_df) +
geom_function(fun = ndist_function,
args = list(gen1$mu, gen1$sigma, gen1$lambda),
colour = colours[1], lwd = 1.5)+
geom_function(fun = ndist_function,
args = list(gen2$mu, gen2$sigma, gen2$lambda),
colour = colours[2], lwd = 1.5)+
geom_function(fun = ndist_function,
args = list(gen3$mu, gen3$sigma, gen3$lambda),
colour = colours[3], lwd = 1.5)+
geom_function(fun = ndist_function,
args = list(gen4$mu, gen4$sigma, gen4$lambda),
colour = colours[4], lwd = 1.5)+
facet_wrap(~centralYear, nrow=5)+
xlim(0, 300)

给定年份的单个图看起来不错:

single plot, geom_function

但是使用facet_wrap的组合图却没有:

combined plot, geom_function and facet_wrap

这显然不是我想要的。似乎每个方面都绘制了相同的函数。也许还有比使用 facet_wrap 不同的解决方案。

任何帮助将不胜感激!

最佳答案

适应 this answer根据您的情况,这可以通过 facet_wrap 来实现,方法是使用应显示的面板值“覆盖”geom_function 层的 centralYear 值。此外,我没有逐一添加图层,而是使用 purrr::pmap 循环遍历参数数据集来创建函数图层。这里,覆盖部分是通过将 mutate(fantasy_df,centralYear = .env$centralYear) 传递给 data 参数来实现的。

library(ggplot2)
library(dplyr, warn=FALSE)
library(purrr)

layer_function <- fantasy_df %>%
mutate(color = colours[generation]) %>%
pmap(function(mu, sigma, color, lambda, centralYear, ...) {
geom_function(data = mutate(fantasy_df, centralYear = .env$centralYear),
fun = ndist_function,
args = list(mu, sigma, lambda),
colour = color
)
})

ggplot() +
layer_function +
facet_wrap(~centralYear, nrow = 5) +
xlim(0, 300)

关于R/ggplot2 : geom_function & facet_wrap with multiple sets of parameters per facet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74003880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com