Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado Chart/Diagram with ggplot2

I'm having a spot of difficulty getting this to output right...

Here's what I've tried so far:

sample data:

dat <- data.frame(
variable=c("A","B","A","B"),
Level=c("Top-2","Top-2","Bottom-2","Bottom-2"),
value=c(.2,.3,-.2,-.3)
)

This is the closest I've got so far:

ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="dodge")
## plots offset, as expected
ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="stack") 
# or geom_bar(), default is stack but it overplots
like image 548
Brandon Bertelsen Avatar asked Feb 23 '23 09:02

Brandon Bertelsen


2 Answers

Since 2012, ggplot forbids Error: Mapping a variable to y and also using stat="bin". The solution is:

ggplot(dat, aes(variable, value, fill=Level)) +
    geom_bar(position="identity", stat="identity") 

It also seriously helps if you use a non-symmetric example, otherwise how do you know if you're not looking at the top series mirrored twice?!

dat <- data.frame(
  variable=c("A","B","A","B"),
  Level=c("Top-2","Top-2","Bottom-2","Bottom-2"),
  value=c(.8,.7,-.2,-.3)
  )

gives your desired tornado plot:

your desired tornado plot

like image 195
smci Avatar answered Feb 26 '23 13:02

smci


You could also use + coord_flip() instead of + geom_bar(position="identity")

like image 40
chitra Avatar answered Feb 26 '23 15:02

chitra