根据出现的情况使折线图中的趋势或多或少突出

Making trends in line graphs more or less prominent based on their occurence

这与 Simon 的博客 post - Plotting individual observations and group means with ggplot2 以及我之前的一个问题有关,Snake 先生给出了一个很好的解决方案。

我有一个包含 600 个人的数据集。对于每个人,我都有 4 年的指标值 - 2014 年、2015 年、2016 年、2017 年。该指标的值在 2014 年取值为 0-5,其余年份取值为 1-4。在每一年中,该值可以与上一年保持相同或增加但不能下降。我正在尝试绘制折线图,​​以便每个人都有一个单独的折线图,代表指标值随时间变化的趋势,因此 X 轴是时间,Y 轴是指标值。下面我给出了说明我的问题所需的最少样本数据 17 行(每行是一个人的,总共有 600 rows/individuals)。

df <- data.frame(c(1:17), c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1), 
c(3,3,3,3,3,3,1,1,1,1,1,3,1,2,1,1,1), c(3,3,3,3,4,4,1,1,1,1,1,3,4,2,1,1,3), c(3,4,3,3,4,4,1,1,1,1,1,3,4,2,1,1,3)) 
colnames(df) <- c("individual_id", paste("indicator_level_", 14:17, sep="")) 

我正在使用以下代码来实现这一点 -

library(tidyverse)

df1 <-df %>%
gather(indicator_level_14, indicator_level_15, indicator_level_16, indicator_level_17, key="Level", value = "Level_value")

ggplot(df1, aes(x=Level, y=Level_value, color=as.factor(individual_id))) +
  geom_line(aes(group = individual_id)) +
  theme(axis.text.x = element_text(angle = 50, hjust = 1)) +
  labs(color = "Sample") + theme(legend.position="none")

我遇到的问题是,由于我的关卡非常离散,而且很多人的关卡轨迹在一段时间内都是相同的,所以几个折线图重叠让我很难看出哪些是更突出的轨迹.当我绘制所有 600 个人的折线图时,这种情况会得到加强。有没有办法让每个线图的粗细与重叠轨迹的数量成正比?

library(dplyr)

df1 %>% 
  group_by(Level, Level_value) %>%               # Find each unique leg
  mutate(count = length(individual_id)) %>%      # Count how many are on each leg
  ggplot(aes(Level, Level_value, group = individual_id, size = count)) + 
  geom_line() + 
  scale_size_continuous(range = c(0.5,3))


df1 %>%
  ggplot(aes(Level, Level_value, group = individual_id)) + 
  geom_line(alpha = 0.25, size = 2)


 df %>%
  group_by(indicator_level_14,               # Find each unique full path
           indicator_level_15, 
           indicator_level_16, 
           indicator_level_17) %>% 
  mutate(count = length(individual_id)) %>%  # Count indivs on each path
  gather(Level, Level_value, -individual_id, -count) %>% 
  ggplot(aes(Level, Level_value, group = individual_id, size = count)) + 
  geom_line() + 
  scale_size_continuous(range = c(0.5,3))