- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 ggplot2::geom_function() 来绘制数据框中一组给定参数的函数。然后应通过“年份”参数将它们组织成各个方面。 StackOverflow 上有类似问题的解决方案( here 和 here ),但我认为我的用例稍微复杂一些。
我的输入是一个数据框。它包含我想要绘制的不同正态分布(mu、sigma、lambda
)的参数,以及我想用作facet_wrap 的facets 的centralYear
参数。除了其他用例之外,我还想绘制每个方面的几个正态分布,这些正态分布由另一个参数生成
来区分。每年的世代数量可能会有所不同。数据框如下所示:
我想要的是如下所示的输出:
这是一个包含测试数据框的最小工作示例:
# 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)
给定年份的单个图看起来不错:
但是使用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/
有没有办法控制 facet_wrap() 图中单个图的大小/纵横比?谢谢, -D 最佳答案 是否可以简单地“释放”x 轴、y 轴或两者的解决方案scales与 scales = "free_x" ,
据我所知 facet_wrap按行填充。以同样的方式,您可以指定如何填充 matrix与 byrow我希望你可以用 facet_wrap 做同样的事情。我知道我可以重新排序一个因子的水平以这种方式绘制
我想生成一个facet_wrap,其中构面内因子的顺序是基于列因子顺序之一。问题的核心是每个组都有重复的因子水平,当我绘制时,facet_wrap 中仅正确排序了一个因子水平。 (见下图) 我尝试对每
简而言之:我想为使用 facet_wrap 制作的双面板图的每个“面板”设置单独的图例。使用 facet_wrap(scales="free") 在我想要不同的轴比例时效果很好,但不适用于点的大小。
如何让每个新行都以新的因子水平开始?目前它基于批处理和样本进行包装,但不会在新的批处理因子级别中断。尝试“facet_grid(~batch~sample)”时,有许多不需要的空面板。 R 的结果和
我正在尝试使用 facet_wrap 将标签(a、b、c...)添加到实际图的边缘之外的图。我希望将它们放在标签区域中,但与 facet_wrap 标签(这是数据组的名称)分开。我可以使用 coord
我正在尝试使用 facet_wrap 绘图,默认情况下按字母顺序排列绘图。但是,所需的结果是按数字降序排列。 下面是我得到的: library(tidyverse) M % ggplot(ae
我的目标是让图中所有国家之间的距离大致相等。例如,在第一类(增加)中,国家是分散的。另一方面,第二类和第三类国家之间的距离太近了。 这迫使我减小国家文本的大小(例如“IS”、“UK”...)和绘制的估
我正在使用 ggplot2 制作几个直方图和 facet_wrap并想在每个面板上绘制平均值。下面,我创建了一个虚拟数据框,找到每个方面的平均值,然后使用 geom_point 创建添加平均值的图。
我尝试根据与相应 facet_wrap 图中相同的数据,将直方图 suplots 添加到 facet_wrap 图的每个部分 geom_sf 图中。 我通过 Google 找到了一些方法,但到目前为止
在以下使用 facet_wrap , 双方 year和 model显示在绘图标签中。 library(tidyverse) mpg %>% filter(manufacturer=='audi')
我写了一个函数来绘制条形图。但是当我开始多面包装时,'~' 符号让事情变得困难。 rf.funct <- function(dat, predictor, feature){ ggplot(get
我有以下数据集: structure(list(Geschaeft = c(0.0961028525512254, 0.0753516756309475, 0, 0.0722803347280335,
假设我有这些数据: set.seed(100) mydf<- data.frame( day = rep(1:5, each=20), id = rep(LETTERS[1:4],25), x = r
我正在尝试在 ggplot 中创建图表使用 facet_wrap争论。 但是,我不想在每个小图上都有标签,我只想在图的顶部和左侧有一个标签。 例如在下图中,我想在顶部标签 SI2、SI1、WS2 和左
数据框 (借自 here): df.test <- data.frame(id = rep(1:6, each = 50), x = rnorm(50*6, mean = 10, sd = 5),
我的 ggplot 有以下代码 - facet_wrap 函数在页面上为每个 Name 绘制 20 个图,沿 x 轴有 5 个 Pcode。我想计算每个名称的平均 TE.Contr 并将该值绘制为每个
我试图实现的是让条形按每个面板的给定变量排序。 一个简单的例子: library(ggplot2) library(plyr) df <- data.frame(fac = c
我正在努力为 facet_wrap() 中的每个方面绘制渐变色标独立。 数据太大,无法在此处发布,但这是我的代码: ggplot(stack, aes(hour, day)) + geom_til
我正在尝试使用facet_wrap和ggplot2将多个标题添加到绘图中。说您例如拥有过去两年的季度数据,并且希望将季度数据与两个主要标题进行图形化比较; 2014年和2015年,以及相应季度的标题。
我是一名优秀的程序员,十分优秀!