R中的部分颜色直方图 [英] Partially color histogram in R

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

问题描述

>

直方图如图所示。我想要两个区域中的条分别被着色为红色和绿色,即,从0到左边的第一个黑色边界的条应该被着色为红色,而第二个区域中的条应该被着色为绿色。
这可以在R中完成吗?
用于获取直方图的代码是

I have a histogram as shown in the picture. I want the bars in the two regions to be coloured red and green respectively, i.e., the bars from 0 to the first black border on the left should be coloured red and the bars in the second region should be coloured green. Can this be done in R? The code used to get the histogram is

hist(pr4$x[pr4$x[,1]>-2,1],breaks=100)


推荐答案

最好的方法是让 hist 为你做计算,然后再次使用 hist 做实际绘图。这里有一个例子:

The best way to do this is to allow hist to do the calculations for you but then use hist (again) to do the actual plotting. Here's an example:

set.seed(1)
x <- rnorm(1000)
h <- hist(rnorm(1000), breaks=50, plot=FALSE)
cuts <- cut(h$breaks, c(-Inf,-.5,1.75,Inf))
plot(h, col=cuts)

最后一行中的.5和1.75是阈值,其中你想要不同的颜色。

The .5 and 1.75 in the last line are the threshold values where you want to have different colors.

注意:我的原始答案使用 barplot

Note: my original answer used barplot, but that strategy makes the axes kind of difficult to work with.

以下是结果:

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

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