繁体   English   中英

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

[英]Line graph plotting several levels of indicator over time in x-axis

我有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")

这对你来说可以接受吗?

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")

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM