Ggplot2: show time difference with arrow

I have a data set with dozens of questions asked over two years. Each question has a value of 2015 and a value of 2016. I would like to build each, and then show the difference between the value of 2015 and the value of 2016. Was the score up or down or stayed the same? I thought it would be useful to connect pairs of points with a line (or arrow) to show the direction of the changes, but I find it hard to get ggplot for this. Here is my sample code:

df <- read.table(text = "question y2015 y2016 q1 90 50 q2 80 60 q3 70 90 q4 90 60 q5 30 20", header = TRUE) g1 <- ggplot(df, aes(x=question)) g1 <- g1 + geom_point(aes(y=y2015, color="y2015"), size=4) g1 <- g1 + geom_point(aes(y=y2016, color="y2016"), size=4) g1 

Different approaches to visualizing this are welcome.

+5
source share
4 answers

It is still a little ugly and needs fine tuning, but it got arrows;)

 library(ggplot2) library(reshape2) library(dplyr) ggplot2df <- read.table(text = "question y2015 y2016 q1 90 50 q2 80 60 q3 70 90 q4 90 60 q5 30 20", header = TRUE) df <- ggplot2df %>% mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down"))%>% melt(id = c("question", "direction")) g1 <- ggplot(df, aes(x=question, y = value, color = variable, group = question )) + geom_point(size=4) + geom_path(aes(color = direction), arrow=arrow()) 

enter image description here

+2
source

I think the dumbbell chart will work as well. Here I redid your data for a long time.

 df <- read.table(text = "question y2015 y2016 q1 90 50 q2 80 60 q3 70 90 q4 90 60 q5 30 20", header = TRUE) df.long <- reshape(df, varying = names(df)[2:3], direction = 'long', #ids = 'question', times = 2015:2016, v.names = 'perc', timevar = 'year' ) ggplot(df.long, aes(x = perc, y = question))+ geom_line(aes(group = question))+ geom_point(aes(colour = factor(year)), size = 2)+ theme_bw()+ scale_color_brewer(palette = 'Set1', name = 'Year') 

enter image description here

+3
source

If you outline the question and put the year on the x axis, you can highlight the direction of the trend with color and use the x axis to show the travel time.

 library(reshape2) library(dplyr) library(ggthemes) ggplot(df %>% melt(id.var="question") %>% group_by(question) %>% mutate(Direction=ifelse(diff(value)>0,"Up","Down")), aes(x=gsub("y","",variable), y=value, color=Direction, group=question)) + geom_point(size=2) + geom_path(arrow=arrow(length=unit(0.1,"in")), show.legend=FALSE) + facet_grid(. ~ question) + theme_tufte() + theme(strip.text.x=element_text(size=15)) + guides(color=guide_legend(reverse=TRUE)) + scale_y_continuous(limits=c(0,100)) + labs(x="Year", y="Value") 

With this aesthetic encoding, you probably don't need a legend, and adding arrows to line segments may also be redundant, but I left them to illustrate.

enter image description here

+3
source

Maybe something like this? Some data reconstruction was necessary and took care of the gather function from the very useful tidyr library.

 library(tidyr) library(ggplot2) g1 <- df %>% gather(year, value, y2015:y2016) %>% ggplot(aes(x = year, y = value, color= question)) + geom_point() + geom_line(aes(group=interaction(question))) g1 

enter image description here

+1
source

All Articles