使用geom_ribbon打印比例

avwztpqn  于 2023-09-27  发布在  其他
关注(0)|答案(2)|浏览(94)

我试图展示不同年龄阶层的比例是如何随着时间而变化的。目前,对于所有数据,我有给定年龄的个体比例(pAge),以及我在geom_ribbon中使用的每个年龄的最小/最大值(cpAgeMin和cpAgeMax)的比例累积和。
不幸的是,即使所有的比例总和为1,我的图似乎并没有正确地反映数据,因为在几年内有一些没有数据的空白点。这些似乎不是数据的问题,而是ggplot正在做的事情。我如何解决这个问题,以便将每年0-1的整个区域分配给一个年龄组。

# subset of data
df <- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
                                  levels = "G", class = "factor"), 
                 year = c(1988, 1988, 1989, 1989, 1989, 1990, 1990, 1991, 1991, 1991, 
                          1992, 1992, 1992, 1993, 1993, 1993, 1998, 1998, 1998, 1999, 
                          1999, 1999, 2000, 2000, 2000, 2001, 2001, 2001, 2002, 2002, 
                          2003, 2003, 2003), 
                 age = c(`9104` = 1, `9341` = 2, `9292` = 1, `9632` = 2, `9960` = 4, 
                         `9543` = 1, `9857` = 3, `9685` = 1, `9968` = 2, `10169` = 3, 
                         `9858` = 1, `10127` = 2, `10212` = 3, `10009` = 1, `10284` = 2, 
                         `10522` = 6, `10464` = 1, `10605` = 2, `10830` = 3, `10556` = 1, 
                         `10744` = 2, `11056` = 3, `10687` = 1, `11061` = 2, `11279` = 5, 
                         `10912` = 1, `11109` = 2, `11197` = 3, `11065` = 1, `11194` = 2, 
                         `11154` = 1, `11255` = 2, `11324` = 3), 
                 pAGE = c(0.972, 0.028, 0.964, 0.005, 0.031, 0.823, 0.177, 0.921, 0.074, 
                          0.004, 0.846, 0.045, 0.109, 0.833, 0.155, 0.012, 0.927, 0.054, 
                          0.019, 0.784, 0.21, 0.005, 0.958, 0.017, 0.025, 0.852, 0.124, 
                          0.024, 0.913, 0.087, 0.909, 0.073, 0.019), 
                 cpAgeMin = c(0, 0.972, 0, 0.964, 0.969, 0, 0.823, 0, 0.921, 0.996, 0, 
                              0.846, 0.891, 0, 0.833, 0.988, 0, 0.927, 0.981, 0, 0.784, 
                              0.995, 0, 0.958, 0.975, 0, 0.852, 0.976, 0, 0.913, 0, 0.909, 
                              0.981), 
                 cpAgeMax = c(0.972, 1, 0.964, 0.969, 1, 0.823, 1, 0.921, 0.996, 1, 0.846, 
                              0.891, 1, 0.833, 0.988, 1, 0.927, 0.981, 1, 0.784, 0.995, 1, 
                              0.958, 0.975, 1, 0.852, 0.976, 1, 0.913, 1, 0.909, 0.981, 1),
                 seg = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
                         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), 
            row.names = c(NA, -33L), class = c("tbl_df", "tbl", "data.frame"))

# plot
ggplot(data = df) + 
  geom_ribbon(aes(x = year, ymin = cpAgeMin, ymax = cpAgeMax, group = interaction(seg, factor(age)), fill = factor(age), col = factor(age))) +
  theme_bw()

更新:

在对第二个站点实施@Andy巴克斯特的解决方案后,图中出现的数据范围之外有额外的“凸起”。我该怎么摆脱这些?

yqhsw0fo

yqhsw0fo1#

将其与geom_areaposition = "fill"叠加可能是最平滑的:

library(tidyverse)

df |> 
  ggplot() +
  geom_area(
    aes(
    x = year,
    y = pAGE,
    group = interaction(seg, factor(age)),
    fill = factor(age)
    ),
    position = position_fill(reverse = TRUE)  # to get right order
  )

bf1o4zei

bf1o4zei2#

好吧,我不喜欢这个解决方案,但这里有一些东西可以填补缺失的值。基本上,我们需要使用complete将隐式NA转换为显式NA,但随后我们需要以“zig-zag”方式填充内容,而不是直接向下填充一列。可能有更好的方法,但我想到的是:

df %>%
  complete(year, age) %>%
  mutate(cpAgeMin=ifelse(is.na(cpAgeMin), lag(cpAgeMax), cpAgeMin)) %>%
  mutate(cpAgeMax=ifelse(is.na(cpAgeMax), cpAgeMin, cpAgeMax)) %>%
  mutate(cpAgeMin=ifelse(is.na(cpAgeMin), lag(cpAgeMax), cpAgeMin)) %>%
  mutate(cpAgeMax=ifelse(is.na(cpAgeMax), cpAgeMin, cpAgeMax)) %>%
  mutate(cpAgeMin=ifelse(is.na(cpAgeMin), lag(cpAgeMax), cpAgeMin)) %>%
  mutate(cpAgeMax=ifelse(is.na(cpAgeMax), cpAgeMin, cpAgeMax)) %>%
  mutate(cpAgeMin=ifelse(is.na(cpAgeMin), lag(cpAgeMax), cpAgeMin)) %>%
  mutate(cpAgeMax=ifelse(is.na(cpAgeMax), cpAgeMin, cpAgeMax)) %>%
  ggplot() + 
  geom_ribbon(aes(x = year, ymin = cpAgeMin, ymax = cpAgeMax, group = factor(age), fill = factor(age), col = factor(age))) +
  theme_bw()

产生

相关问题