Tornado plot in R

余生长醉 提交于 2019-12-24 01:27:56

问题


I am trying to make a tornado plot (a.k.a. sensitivity graph) in R. The goal is to visualize the effect of a 10% increase and 10% decrease in some variables.

So far I have gotten this result

This is the code I am using:

# Tornado plot

data <- matrix(c(-0.02,0.02,-0.01,0.01,-0.03,0.02,-0.01,0.04), ncol = 4)
rownames(data) <- c('+10%','-10%')                       # Amount of change in variables
colnames(data) <- c('V_bar', 'alpha', 'rho','xi')        # Names of variables
x <- seq(-0.04,0.04, length=10)                          # For plotting '%' on x-axis

barplot(data, horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab = '',
        beside=T, col=c('springgreen','indianred2'))
axis(1, at=pretty(x),  lab=paste0(pretty(x) * 100," %"), las=TRUE)

I have two final goals I want to achieve:

  1. Getting the bars aligned for each variable (not juxaposed as they are now). In other words, the green and red side of each variable bar should meet at the center, giving a total of four bars.

  2. Paste mathematical characters (instead of the text) on the y-axis. The "V_bar" column should be a V with a bar on top.

Edit: I have posted a separate question for the mathematical characters: Barplot: Greek letters on y axis in R


回答1:


So you can split the positive and negative case and plot the overlaying each other with add = TRUE

barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab = '',
        beside=T, col=c('springgreen'))
barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab = '',
        beside=T, col=c('indianred2'), add = TRUE)
axis(1, at=pretty(x),  lab=paste0(pretty(x) * 100," %"), las=TRUE)

so we are taking the first row (negative values) for one plot, and the second row (positive values) for second plot. Their order must match and they will be plotted side by side.

For this to work and match nicely results must have the same length and only one colour per graph is specified.

Adding greek character to axis title

this other post might help you with your second question



来源:https://stackoverflow.com/questions/37059281/tornado-plot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!