【问题标题】:How to partly colorize histogram?如何对直方图进行部分着色?
【发布时间】:2020-08-15 21:50:51
【问题描述】:

我一直在尝试在以下数据帧 (df) 中为高于定义阈值的特定 bin 着色

df <- read.table("https://pastebin.com/raw/3En2GWG6", header=T)

我一直在关注这个例子 (Change colour of specific histogram bins in R),但我似乎无法让他们的建议适应我的数据,所以我想在 stackoverflow 上问你

我希望所有值高于 0.100 的 bin 都为“红色”,其余的要么没有颜色,要么只是黑色(我定义了黑色,但我更喜欢没有颜色)

这是我尝试过的:

col<-(df$consumption>=0.100)
table(col) # I can see 40 points above 100, the rest below

col[which(col=="TRUE")] <- "firebrick1"
col[which(col=="FALSE")] <- "black"

hist(df$consumption, breaks = 1000, xlim = c(0,0.2), col=col,xlab= "Consumption [MG]")

但是,整个图表都是红色的,这没有意义..?

换句话说,我希望下一行右侧的 anything 为红色

hist(df$consumption, breaks = 1000, xlim = c(0,0.2),xlab= "Consumption [MG]")
abline(v=c(.100), col=c("red"),lty=c(1), lwd=c(5))

【问题讨论】:

    标签: r histogram


    【解决方案1】:

    使用add=TRUE 简单地绘制两个直方图并设置第二个。

    hist(df$consumption, breaks=1000, xlim=c(0,.2),xlab= "Consumption [MG]")
    hist(df$consumption[df$consumption > .100], breaks=1000, xlim=c(0,.2), col=2, add=TRUE)
    
    abline(v=.100, col=2, lty=3)
    

    【讨论】:

    • 非常感谢。我想到了这一点,并制作了一个新的df,只有0.100以上的点,绘制它并包含add = T,但它不起作用(尺寸(=)有问题。但是你的方法非常好,非常很容易理解,现在我看到了你如何隔离 >0.100 的垃圾箱
    【解决方案2】:

    这与您所做的大致相同。您不想计算截止值之上的点,而是计算截止值之上的直方图箱数。

    # store the histogram as an object
    h <- hist(df$consumption, breaks = 1000)
    
    # extract out the breaks, and assign a color vector accordingly
    cols <- ifelse(h$breaks > 0.1, "firebrick1", "black")
    
    # use the color vector
    plot(h, col = cols, xlim=c(0,.2),xlab= "Consumption [MG]")
    abline(v=c(.100), col=c("red"),lty=c(1), lwd=c(5))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多