R中的龙卷风图 [英] Tornado plot in R

查看:333
本文介绍了R中的龙卷风图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中绘制龙卷风图(又称灵敏度图).目标是可视化某些变量增加10%和减少10%的效果.

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. 获取与每个变量对齐的条形图(现在不并列).换句话说,每个可变条的绿色和红色边应在中心相交,总共有四个条.

  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.

在y轴上粘贴数学字符(而不是文本). "V_bar"列应为V,顶部带有一个横条.

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

编辑:我已经发布了一个有关数学字符的单独问题:

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

推荐答案

因此,您可以拆分正例和负例,并使用add = TRUE

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.

在轴标题中添加希腊字符

另一篇文章可能会帮助您解决第二个问题

this other post might help you with your second question

这篇关于R中的龙卷风图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆