简体   繁体   中英

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

I have a data set of 600 respondents. For each respondent I want to track the level of an indicator over time using a line graph. The indicator is divided in 5 levels - 0,1,2,3,4, and I have the indicator for 4 years - 2014, 2015, 2016 and 2017. So I want the sample number on y-axis, and one line representing each respondent across the 4 time periods indicating the level of indicator. How can this be possible? I appreciate your help! I want to further facet out this graph using deciles of income in the income variable.

Sample dataframe:

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

Would this be acceptable to you?

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

I only use 3 different clusters (instead of 10) based on quantile because of the number of rows.

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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