根据计数改变线图的厚度

Altering thickness of line graph based on counts

Dataframe“id”有 year、id 和 matriline 列,其中每一行都是一个事件。我想按母系统计每年发生的事件数量,所以我做了:

events.bymatr = 
id %>%
group_by(year, matr, .drop = FALSE) %>%
dplyr::summarise(n = n()) %>%
ungroup()
events.bymatr

我用母系绘制了随时间变化的事件数量的折线图。

ggplot(events.bymatr, aes(x=year, y=n, group=matr)) + geom_line(aes(color=matr))

我的问题是双重的:

  1. 有没有一种方法可以重新创建此折线图,其中线条的粗细取决于每个母系的 ID 数量?我想这会涉及重塑上面的数据,但是当我尝试 group_by(year,matr,id,.drop=FALSE) 时,我的数据全都不稳定。

  2. 我想更改调色板,使每种颜色都非常不同 - 如何添加新的调色板?我尝试将 this c25 palette 与此代码一起使用,但它使我所有的行都消失了。 ggplot(events.bymatr, aes(x=year, y=n, group=matr)) + geom_line(aes(color=c25))

非常感谢!

“id”的输出(缩短为每列的前五行):

> dput(id)
    structure(list(date = structure(c(8243, 8243, 8243, 8248, 8947,
    class = "Date"), year = c(1992L, 1992L, 1992L, 1992L, 1994L), 
    event.id = c(8L, 8L, 8L, 10L, 11L), id = structure(c(51L, 55L, 59L, 
    46L, 51L), .Label = c("J11", "J16", "J17", "J2", "J22"),
    class = "factor"), sex = structure(c(1L, 2L, 2L, 1L, 1L),
    .Label = c("0", "1"), class = "factor"), age = c(28L, 12L, 6L, 42L, 
    30L), matr = structure(c(20L, 20L, 20L, 11L, 20L), .Label = c("J2", 
    "J4", "J7", "J9", "K11"), class = "factor"),
    matralive = structure(c(2L, 2L, 2L, 2L, 2L),
    .Label = c("0", "1"), class = "factor"), pod = structure(c(3L, 3L, 
    3L, 3L, 3L), .Label = c("J", "K", "L"), class = "factor")),
    row.names = c(NA, -134L), class = c("tbl_df", "tbl", "data.frame"))

events.bymatr 的输出:

> dput(events.bymatr)
      structure(list(year = c(1992L, 1992L, 1992L, 1992L, 1992L),
      matr = structure(c(1L, 2L, 3L, 4L, 5L), .Label = c("J2", "J4", 
      "J7", "J9", "K11"), class = "factor"), n = c(0L, 0L, 0L, 0L, 0L)), 
      row.names = c(NA, -380L), class = c("tbl_df", "tbl", 
      "data.frame"))

正如@r2evans 指出的那样,要在多种颜色之间清楚地区分出奇地困难。我在这里使用了一个示例 20 色标,它做得很好,但即便如此,也有一些很难区分。这是使用 dplyr 中包含的 storms 数据集的尝试。

library(dplyr)
storms %>%
  group_by(name, year) %>%
  summarize(n = n(), .groups = "drop") %>%   # = number of name per year View
  tidyr::complete(name, year = 1975:2015, fill = list(n = 0)) %>% 
  group_by(name) %>%
  mutate(total = sum(n)) %>%                 # = number of name overall
  ungroup()  %>% 
  filter(total %% 12 == 0) %>% # Arbitrary, to reduce scope of data for example
  ggplot(aes(year, n, color = name, size = total, group = name)) +
  geom_line() +
  guides(color = guide_legend(override.aes = list(size = 3))) +
  ggthemes::scale_color_tableau(palette = "Tableau 20")