plot vertical stacking line based on time point in r -
my df this:
test id c1 c2 c3 c4 c5 c6 c7 22 112 112 118 121 124 na na b 22 77 89 85 89 88 95 100 c 22 67 85 76 77 77 84 92 d 22 58 81 73 75 79 84 95
c1, c2, c3... represents different time points. each row represents different test in df, student 22 has been tested 5 times on test a, , 7 times on test b,c , d.
i intend use ggplot2 create vertically stacked line graph x-axis 4 tests, y-axis scores, , vertical stacking being based on time point. can me?
thanks!
may helps
library(tidyr) library(ggplot2) gather(df, var, val, c1:c7) %>% filter(!is.na(val)) %>% ggplot(. , aes(x=test, y=val, fill=var))+ geom_bar(stat='identity')
update
these options.
df1 <- gather(df, var, val, c1:c7) %>% filter(!is.na(val)) ggplot(df1, aes(x=test, y=val, colour=var))+ geom_area(aes(fill=var), position='stack')
or
ggplot(df1, aes(x=as.numeric(factor(test)), y=val, fill=var)) + geom_area(position='stack')
or
group_by(df1, test) %>% mutate(val1=cumsum(val)) %>% ggplot(., aes(x=as.numeric(factor(test)), y=val1, color=var)) + geom_line() + xlab('test') + ylab('score') + scale_x_discrete(breaks=as.numeric(factor(df2$test)), labels=df2$test)
data
df <- structure(list(test = c("a", "b", "c", "d"), id = c(22l, 22l, 22l, 22l), c1 = c(112l, 77l, 67l, 58l), c2 = c(112l, 89l, 85l, 81l), c3 = c(118l, 85l, 76l, 73l), c4 = c(121l, 89l, 77l, 75l ), c5 = c(124l, 88l, 77l, 79l), c6 = c(na, 95l, 84l, 84l), c7 = c(na, 100l, 92l, 95l)), .names = c("test", "id", "c1", "c2", "c3", "c4", "c5", "c6", "c7"), class = "data.frame", row.names = c(na, -4l))
Comments
Post a Comment