如何在R中制作渐变色条来表示风险分析结果? [英] How to make a gradual color bar for representation of risk analysis results in R?

查看:106
本文介绍了如何在R中制作渐变色条来表示风险分析结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在渐变色条(绿-黄-红色)上绘制按4个级别(无风险,低风险,中风险和高风险)分类的风险分析结果.我曾想过类似的事情(但我愿意接受更好的建议.)

I want to plot results of risk analysis that categorized by 4 levels (No risk, low, moderate and high risk) on gradual color bar (green-yellow-red). I thought about something like this (but I'm open for nicer suggestions.)

输入内容是这样的

results <- data.frame(matrix(nrow = 5,ncol = 0))
results$day <- c(1:5)
results$assessment <-c("Low","Moderate","High","Low","Moderate")

推荐答案

我将尝试用答案给出很多细节.

I will try to give a lot of detail with my answer.

我假设您只是想要 颜色条,而不是颜色条添加到其他图形.这就是下面创建的.

I am assuming that you want just the colorbar and not the colorbar added to some other graph. That is what is created below.

library(fields)     ## for the colorbar function

## create the palette
myPalette = colorRampPalette(c("green", "yellow", "orange", "red"))

1创建一个空图.

plot(0:1, 0:1, type="n", bty="n", xaxs="i",
    xaxt="n", yaxt="n", xlab="", ylab="")   

运行此命令时,应该得到一个空图-没有轴,什么也没有.x和y的范围均为0到1.

When you run this, you should get an empty plot - no axes, nothing. Both x and y range from 0 to 1.

colorbar.plot(0.5, 0.05, 1:100, col=myPalette(100),
    strip.width = 0.2, strip.length = 1.1)

3添加轴标签

axis(side=1, at=seq(0,1,1/3), tick=FALSE,
    labels=c("No Risk", "Low", "Moderate", "High Risk")) 

4添加箭头.

arrows(0.7, 0.18, 0.7, 0.1, length=0, lwd=8)
arrows(0.7, 0.18, 0.7, 0.09, length=0.1, lwd=3, angle=45)

这有点骇人听闻.如果我将箭头中的线加粗,
箭头是相当钝和丑陋的.因此,第一个箭头语句使没有箭头的粗线和第二个使用细线并添加尖锐的箭头.

This is a bit of a hack. If I made the lines in the arrow thick,
the arrowhead was rather blunt and ugly. So the first arrows statement makes a thick line with no arrowhead and the second one uses a thin line and adds a sharp arrowhead.

我的箭头位于0.7.调整x值以将其放置在其他位置.

I have the arrow at 0.7. Adjust the x values to place it elsewhere.

这篇关于如何在R中制作渐变色条来表示风险分析结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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