r - Line graph plotting several levels of indicator over time in x-axis -
i have data set of 600 respondents. each respondent want track level of indicator on time using line graph. indicator divided in 5 levels - 0,1,2,3,4, , have indicator 4 years - 2014, 2015, 2016 , 2017. want sample number on y-axis, , 1 line representing each respondent across 4 time periods indicating level of indicator. how can possible? appreciate help! want further facet out graph using deciles of income in 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 acceptable you?
library(ggplot2) library(dplyr) library(tidyr) library(magrittr)
i use 3 different clusters (instead of 10) based on quantile because of 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")
Comments
Post a Comment