Brandon Bertelsen
Brandon Bertelsen

Reputation: 44638

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

Upvotes: 4

Views: 7146

Answers (3)

Idan Richman
Idan Richman

Reputation: 673

If the minus values are just a trick to compare two groups, you can use:

scale_y_continuous(labels=abs)

Upvotes: 0

smci
smci

Reputation: 33940

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

Upvotes: 7

chitra
chitra

Reputation: 21

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

Upvotes: 2

Related Questions