【问题标题】:Finding multiple peak densities on facet wrapped ggplot for two datasets在两个数据集的 facet Wrapped ggplot 上查找多个峰值密度
【发布时间】:2021-09-25 22:34:03
【问题描述】:

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

year julian group
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很陌生。

【问题讨论】:

    标签: r ggplot2 facet-wrap kernel-density density-plot


    【解决方案1】:

    我建议不要将所有计算都集中到一行代码中,而是将其拆分为这样的步骤。我没有使用您的代码来查找最高峰,而是使用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)
    

    【讨论】:

    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多