更改R中特定直方图容器的颜色 [英] Change colour of specific histogram bins in R

查看:54
本文介绍了更改R中特定直方图容器的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用plot函数创建了下面的历史图:

I have created the below historgram using the plot function:

hist(mst$Total[which(mst$Total<100)], axes = TRUE, ylab = "", xlab = "", main = "", col = "chartreuse4", breaks = tb, freq=TRUE, right = FALSE)

已将中断指定为以下内容:

The breaks have been specified as the following:

tb = c(0,1,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)

我希望能够用不同的颜色填充特定的直方图箱.

I would like to be able to fill specific historgram bins with different colours.

第一种颜色:[0,1)

colour one: [0,1)

第二种颜色:[1,5)

colour two: [1,5)

颜色三:[5,10)[10,15)和[15,20)

colour three: [5,10) [10,15) and [15,20)

颜色四:从[20,25]开始的所有垃圾箱

colour four: All bins from [20,25] onwards

我该如何使用r基本封装图形来做到这一点?

How can I do this using r base package graphics?

非常感谢

更新为接受的答案:

使用接受的答案,我可以创建以下图表:

Using the accepted answer I was able to create the below plot:

range <- c(0, 1, 5, 20, 100)
col <- findInterval(tb, range, all.inside = TRUE)
col[which(col==1)] <- "firebrick1"

col[which(col==2)] <- "gold"
col[which(col==3)] <- "darkolivegreen1"
col[which(col==4)] <- "forestgreen"
hist(mst$Total[which(mst$Total<100)], axes = TRUE, ylab = "", xlab = "", main = "", col = col, breaks = tb, freq=TRUE, right = FALSE)

推荐答案

首先我们定义您的颜色间隔

First we define your color intervals

tb <- c(0,1,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
range <- c(0, 1, 5, 20, 100)

然后安排您的休息时间

col <- findInterval(tb, range, all.inside = TRUE)
col
[1] 1 2 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

然后您可以使用 col 为每个垃圾箱着色

You can then use col to color each bin

hist(mst$Total[which(mst$Total<100)], axes = TRUE, ylab = "", xlab = "", main = "", col = col, breaks = tb, freq=TRUE, right = FALSE)

很遗憾,由于您没有提供数据,因此我无法对其进行测试,但这应该可以工作.

Unfortunately I can't test it since you provided no data, but this should work.

这篇关于更改R中特定直方图容器的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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