开发者

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


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:

Tornado Chart/Diagram with ggplot2


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


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

scale_y_continuous(labels=abs)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜