Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: Show difference in values over time with an arrow

Tags:

r

ggplot2

I have a data set with scores to questions asked over two years. Each question has a 2015 value and a 2016 value. I would like to plot each and then show the different between the 2015 value and the 2016 value. Did the score go up or down or stay the same? I was thinking it might be useful to connect pairs of points with a line (or an arrow) to show the direction of change, but I'm having a hard time getting ggplot to do this. Here is my code example:

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.

like image 851
oneself Avatar asked Jun 29 '16 16:06

oneself


People also ask

What does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

What does geom_point mean?

geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.

Is geom_point a scatter plot?

The geom_point function can be used to create a basic scatter plot in ggplot2.

What is cyl in Ggplot?

cyl is a discrete variable. To get ggplot to recognize it as such we need convert it to a factor. p + geom_point(aes(color = factor(cyl))) Later we'll change the label of the legend. Another way to plot discrete variables is with shape.


2 Answers

If you facet by question and put year on the x-axis, you can highlight the trend direction with color and use the x-axis to show the passage of 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 encoding of aesthetics, you probably don't need the legend, and adding arrows to the line segments may be superfluous as well, but I've left them in for illustration.

enter image description here

like image 113
eipi10 Avatar answered Nov 06 '22 19:11

eipi10


I think a "dumbbell" chart would work, too. Here I've reshaped your data to long.

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

like image 30
bouncyball Avatar answered Nov 06 '22 20:11

bouncyball