Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference plot

Tags:

r

ggplot2

I don't know the name of this type of plot (comments around this are welcomed). Essentially it is a barplot with glyphs that are filled to indicate a loss/gain. The glyph is arrow like encoding information about direction, magnitude, and allowing the bar geom under to be seen.

enter image description here

This looks interesting but can't think of how to do it in ggplot2 (grid frame work). How could we recreate this plot in ggplot2/grid framework (base solutions welcomed as well for completeness of question). Specifically the glyphs, not the text as this is pretty straight forward in ggplot2 already.

Here is some code to create data and traditional overlaid & coordinate flipped dodged bar plots and line graphs to show typical ways of visualizing this type of data.

set.seed(10)
x <- sample(30:60, 12)
y <- jitter(x, 60)

library(ggplot2)
dat <- data.frame(
    year = rep(2012:2013, each=12),
    month = rep(month.abb, 2),
    profit = c(x, y)
)


ggplot() + 
geom_bar(data=subset(dat, year==2012), aes(x=month, weight=profit)) +
geom_bar(data=subset(dat, year==2013), aes(x=month, weight=profit), width=.5, fill="red")

ggplot(dat, aes(x=month, fill=factor(year))) +
    geom_bar(position="dodge", aes(weight=profit)) +
    coord_flip

ggplot(dat, aes(x=month, y=profit, group = year, color=factor(year))) +
    geom_line(size=1) 

enter image description here

enter image description here

enter image description here

like image 573
Tyler Rinker Avatar asked Oct 08 '14 03:10

Tyler Rinker


People also ask

What is Bland-Altman plot used for?

A Bland-Altman plot is a useful display of the relationship between two paired variables using the same scale. It allows you to perceive a phenomenon but does not test it, that is, does not give a probability of error on a decision about the variables as would a test.

What is a bias plot?

The B&A plot analysis is a simple way to evaluate a bias between the mean differences, and to estimate an agreement interval, within which 95% of the differences of the second method, compared to the first one fall. Data can be logarithmically transformed, if differences seem not to be normally distributed.


1 Answers

Here is an example, perhaps there are other ways though,

dat <- data.frame(
  year = rep(2012:2013, each=12),
  month = factor(rep(1:12, 2), labels=month.abb),
  profit = c(x, y)
)
dat2 <- reshape2::dcast(dat, month~ year, value.var = "profit")
names(dat2)[2:3] <- paste0("Y", names(dat2)[2:3])

ggplot(dat2) + 
  geom_bar(aes(x=month, y = Y2012), stat = "identity", fill = "grey80", width = 0.6) +
  geom_segment(aes(x=as.numeric(month)-0.4, xend = as.numeric(month)+0.4, y = Y2013, yend = Y2013)) + 
  geom_segment(aes(x = month, xend = month, y = Y2013, yend = Y2012, colour = Y2013 < Y2012), 
               arrow = arrow(60, type = "closed", length = unit(0.1, "inches")), size = 1.5) +
  theme_bw()

enter image description here

like image 63
kohske Avatar answered Oct 19 '22 08:10

kohske