【问题标题】:R: Adjust Color Gradient for 3DBarplot using latticeExtra cloud in RR:使用 R 中的 latticeExtra 云调整 3DBarplot 的颜色渐变
【发布时间】:2021-01-22 00:07:40
【问题描述】:

我正在尝试更改 3d 条形图的颜色,使颜色从绿色变为黄色、橙色、浅红色、红色、深红色。但是,每当我绘制它时,看起来颜色在绿色之后开始重复。有什么,我可以解决这个问题吗?我得到的输出是:

我想要的输出是这样的: enter image description here

这是我正在使用的代码。它给了我第一张照片。那不是想要的。我希望颜色看起来像第二张图片中的样子。

library(latticeExtra)
myPalette <- colorRampPalette(rev(c("red2","tomato2","red4","orange", "gold1","forestgreen")))(6)
k <- read.table(text = 'x y z
NotKnown   0 Critical
NotKnown   0 Substantial
NotKnown   0 Significant
NotKnown   0 Moderate
NotKnown   0 Negligible
NotLikely  2 Critical
NotLikely  5 Substantial
NotLikely  7 Significant
NotLikely  0 Moderate
NotLikely  0 Negligible
Reasonable 0 Critical
Reasonable 1 Substantial
Reasonable 9 Significant
Reasonable 1 Moderate
Reasonable 0 Negligible
Likely     0 Critical
Likely     0 Substantial
Likely     1 Significant
Likely     0 Moderate
Likely     0 Negligible
VeryLikely 0 Critical
VeryLikely 0 Substantial
VeryLikely 0 Significant
VeryLikely 0 Moderate
VeryLikely 0 Negligible',header=TRUE)

k$x <- factor(k$x,levels = c("VeryLikely", "Likely","Reasonable","NotLikely","NotKnown"))
k$z <- factor(k$z,levels= c("Critical","Substantial","Significant","Moderate","Negligible"))

cloud(y~z+x, k, panel.3d.cloud=panel.3dbars, col.facet=myPalette, 
             xbase=0.8, ybase=0.8, xlab= "Severity",ylab="Probability",zlab="Number of Risks",scales=list(arrows=FALSE, col=1), 
             par.settings = list(axis.line = list(col = "transparent")))

【问题讨论】:

  • 你能不能说的更明确一点,你想做什么?
  • 我希望 3D 图的颜色渐变与您在风险矩阵上看到的类似,其中颜色从绿色到橙色、黄色到红色。

标签: r 3d bar-chart heatmap lattice


【解决方案1】:

我想这就是你要找的东西

assign.color <- function(coordinate) {
  if (sum(coordinate) == 2)
    return("red4")
  
  if (sum(coordinate) == 3)
    return("red2")
  
  if (sum(coordinate) == 4)
    return("tomato2")
  
  if (sum(coordinate) == 5)
    return("orange")
  
  if (sum(coordinate) == 6)
    return("gold1")
  
  if (sum(coordinate) >= 7)
    return("forestgreen")
}

k$x.int <- as.integer(k$x)
k$z.int <- as.integer(k$z)

k$color <- apply(k,
                 MARGIN = 1,
                 FUN = function (row) {
                   return(assign.color(as.integer(c(row["x.int"], row["z.int"]))))
                 })

k$colorCode <- apply(k,
                     MARGIN = 1,
                     FUN = function(row) {
                       return(colorRampPalette(row["color"])(6)[1])
                     })


cloud(y~z+x, k, panel.3d.cloud=panel.3dbars, col.facet=k$colorCode, 
      xbase=0.8, ybase=0.8, xlab= "Severity",ylab="Probability",zlab="Number of Risks",scales=list(arrows=FALSE, col=1), 
      par.settings = list(axis.line = list(col = "transparent")))

产生

关键是要了解函数cloud 中的参数col.facet 也接受vector,它可以与数据点的数量一样长。

【讨论】:

  • 这非常有效。感谢您解释来自 function cloud 的参数 col.facet 也接受一个向量,该向量可以与您的数据点的数量一样长。
猜你喜欢
  • 2016-03-13
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 2020-11-27
  • 2019-08-09
相关资源
最近更新 更多