线图在x轴上绘制多个指标水平

时间:2017-07-26 06:41:13

标签: r ggplot2

我有600名受访者的数据集。对于每个受访者,我想使用折线图跟踪指标随时间的水平。该指标分为5个级别 - 0,1,2,3,4,我有4年的指标 - 2014年,2015年,2016年和2017年。 所以我想要y轴上的样本号,并且在4个时间段内代表每个受访者的一行表示指标的水平。这怎么可能?我感谢您的帮助! 我想用收入变量中的收入十分位数来进一步理清这个图表。

示例数据框:

df <- data.frame(c(1:5), c(0, 1, 0, 2, 2), c(1, 2, 2, 4, 4), c(2, 3, 3, 4, 4), c(3,3,3,4,4), c(10000, 200000, 15000, 40000, 350000) 
colnames(df) <- c("sample_no", paste("indicator_level_", 14:17, sep=""), "annual_income")

1 个答案:

答案 0 :(得分:3)

这对你来说是否可以接受?

library(ggplot2)
library(dplyr)
library(tidyr)
library(magrittr)

由于行数的原因,我只使用基于分位数的3个不同的簇(而不是10个)。

df2 <- df %>%
  mutate(quantile = ntile(annual_income, 3)) %>%
  gather(indicator_level_14, indicator_level_15, indicator_level_16, indicator_level_17, 
         key = "Indicator", value = "Value")


ggplot(df2, aes(x = Indicator, y = Value, color = as.factor(sample_no))) + 
  geom_line(aes(group = sample_no)) +
  facet_wrap(~quantile) +
  theme(axis.text.x = element_text(angle = 50, hjust = 1)) +
  labs(color = "Sample")

enter image description here