gpt4 book ai didi

r - 在两个数据集的 facet wrapped ggplot 上查找多个峰值密度

转载 作者:行者123 更新时间:2023-12-04 07:22:14 26 4
gpt4 key购买 nike

我目前正在尝试绘制每年朱利安日期的苍蝇密度。目的是查看两种数据收集方法(第 1 组和第 2 组)的苍蝇密度何时达到峰值。我有很多行数据,在10年的过程中,例如,数据集是这样的:



朱利安
团体


2000年
214
1

2001年
198
1

2001年
224
1

2000年
189
2

2000年
214
2

2001年
222
2

2001年
259
2

2000年
260
2

2000年
212
1


每一行都是一个观察值。
这是我第一次使用 ggplots 绘图,所以我对如何绘制每年的垂直峰值线感到困惑。
目前的代码如下所示:
代码

data$group <- as.factor(data$group)

plots <- ggplot(data, aes(x = julian, group = group)) +
geom_density(aes(colour = group),adjust = 2) + facet_wrap(~year, ncol = 2)
我尝试使用此代码绘制峰值:
geom_vline(data = vline, aes(xintercept = density(data$julian)$x[which.max(density(data$julian)$y)]))

vline <- summarise(group_by(data,year, group=group), density(ata$julian, group=group)$x[which.max(density(data$julian)$y)])

vline
但是我认为它已经找到了所有年份和所有组的峰值密度。
请有人帮助我建议如何绘制每年和每个方面的组的最大密度?如果有多个峰会更好,我将如何找到这些峰以及峰的定量值?
在此先感谢您,我对 ggplots 很陌生。

最佳答案

与其试图将所有计算都集中在一行代码中,我建议将其拆分为这样的步骤。我没有使用您的代码来查找最高峰,而是使用了 this原则上也应该找到多个峰值的答案(见下文):


library(dplyr)
library(ggplot2)

fun_peak <- function(x, adjust = 2) {
d <- density(x, adjust = adjust)
d$x[c(F, diff(diff(d$y) >= 0) < 0)]
}

vline <- data %>%
group_by(year, group) %>%
summarise(peak = fun_peak(julian))
#> `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.

ggplot(data, aes(x = julian, group = group)) +
geom_density(aes(colour = group), adjust = 2) +
geom_vline(data = vline, aes(xintercept = peak)) +
facet_wrap(~year, ncol = 2)

这是一个基于链接答案中的示例数据的具有多个峰值的小示例:
x <- c(1,1,4,4,9)

data <- data.frame(
year = 2000,
julian = rep(c(1,1,4,4,9), 2),
group = rep(1:2, each = 5)
)
data$group <- as.factor(data$group)

vline <- data %>%
group_by(year, group) %>%
summarise(peak = fun_peak(julian, adjust = 1))
#> `summarise()` has grouped output by 'year', 'group'. You can override using the `.groups` argument.

ggplot(data, aes(x = julian, group = group)) +
geom_density(aes(colour = group), adjust = 1) +
geom_vline(data = vline, aes(xintercept = peak)) +
facet_wrap(~year, ncol = 2)

关于r - 在两个数据集的 facet wrapped ggplot 上查找多个峰值密度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68420556/

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