plot - Visualizing values in different columns for each row and connect them by line in R -
i have following data frame:
user_id 2016-23 2016-23 2016-25 1 10 20 30 2 5 15 45 3 20 11 21 4 40 30 20 5 40 21 17
i need visualize data frame have '2016-23' , '2016-23' , '2016-25' in x axis , connected dots each 'user_id ' based on value in column see if trend increasing or decreasing on time.
if understand correctly, looking for..
library(ggplot2) library(reshape2) df <- data.frame(user_id=c(1,2,3,4,5),a=c(10,5,20,40,40), b=c(20,15,11,30,21),c=c(30,45,21,20,17)) colnames(df)=c("user_id","2016-23","2016-23","2016-25") df<- reshape2::melt(df,"user_id",2:4) df_res <- mutate(df,user_id = as.factor(user_id),variable=as.character(variable))
this results in:
user_id variable value 1 1 2016-23 10 2 2 2016-23 5 3 3 2016-23 20 4 4 2016-23 40 5 5 2016-23 40 6 1 2016-23 10 7 2 2016-23 5 8 3 2016-23 20 9 4 2016-23 40 10 5 2016-23 40 11 1 2016-25 30 12 2 2016-25 45 13 3 2016-25 21 14 4 2016-25 20 15 5 2016-25 17
plotting data
ggplot(df_res,aes(x=variable,y=value,group=user_id,colour=user_id))+geom_line()+geom_point(aes(shape=user_id))
if looking plot trend line can add geom_smooth
follows (since don't have enough data points graph results wouldn't mean anything.
ggplot(df_res,aes(x=variable,y=value,group=user_id,colour=user_id))+geom_line()+geom_point(aes(shape=user_id))+geom_smooth(method = "lm",aes(group=user_id))
Comments
Post a Comment