R:使用 R 中的latticeExtra 云调整 3DBarplot 的颜色梯度 [英] R: Adjust Color Gradient for 3DBarplot using latticeExtra cloud in R

查看:22
本文介绍了R:使用 R 中的latticeExtra 云调整 3DBarplot 的颜色梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要的输出是这样的:

关键是要理解函数cloud 中的参数col.facet 也接受一个vector 可以和数字一样长您的数据点.

I am trying to change the colors of the 3d bar plot so that the colors go from green, to yellow, to orange, to light red, to red, to dark red. However, whenever I plot it, it looks like the colors start repeating after green. Is there something, I can do to fix this? The output that I get is:

The output that I want is this: enter image description here

This is the code, I am using. It gives me the first picture. That is not what want. I want the colors to look the way they look in the second picture.

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")))

解决方案

I think this is what you are looking for

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")))

produces

The key is to understand that the arugment col.facet from function cloud also accepts a vector that can be as long as the number of your data points.

这篇关于R:使用 R 中的latticeExtra 云调整 3DBarplot 的颜色梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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