使用scale_fill_gradientn将特定颜色分配给条形图中的确定值 [英] Assign specific color to definite value in bar plot using scale_fill_gradientn

查看:269
本文介绍了使用scale_fill_gradientn将特定颜色分配给条形图中的确定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的数据集,其中包含20位患者的某些参数的逐分钟记录.通过可视化患者监护记录(IP参数),我试图构造彩色的条形图.因此,我在r中使用了 scale_fill_gradient()函数.

I have a huge dataset containing minute-by minute recordings of some parameters in 20 patients. By visualizing the records of patient's monitoring (The IP parameter) I was trying to construct the colored barplots. So I used the scale_fill_gradient() function in r.

问题是,我想给一个确定的值(例如IP = 20)分配一种特殊的颜色(比如说白色).可以使用scale_fill_gradient做到这一点吗?

The problem is, that I'd like to assign to a definite value (for example IP = 20) a special color (let's say white). Is it possible to do that with scale_fill_gradient?

我的数据库如下:

Patient min IP
1a      75  19
1a      76  21 
1a      77  20
1a      78  18.5
1a      79  17
1a      80  25
1a      81  29.3
1a      82  32.1
1a      83  30.9
2c      1   2
2c      2   5
2c      3   8 
2c      4   9
2c      5   12
2c      6   16   
2c      7   18
3v      72  38 
3v      73  35
3v      74  30.3
3v      75  28.7
3v      76  27
3v      77  25.2
3v      78  22
3v      79  19.1
3v      80  18 
3v      81  15

我的代码是

i<- ggplot(data, aes(patient, IP, fill=IP))
IPcol <- c("green", "greenyellow", "#fed976","#feb24c", "#fd8d3c", "#fc4e2a", "#e31a1c", "#bd0026", "#800026", "black")
i+geom_bar(stat = "identity")+ scale_fill_gradientn(colours = IPcol)+ coord_flip()+scale_y_time()

从此代码中,我得到以下图像:

From this code I obtain the following image:

所以我唯一要更改的-IP = 20应该是白色

So the only thing i want to change - is that IP = 20 should be white

推荐答案

所以简单的选择是将等于20s的 IP s重新编码为 NA s并设置达到该比例的 na.value :

So the easy option would be to recode the IPs that equal 20s to NAs and set a na.value in that scale:

data <- read.table(text = your_posted_data, header = T)


IPcol <- c("green", "greenyellow", "#fed976","#feb24c", "#fd8d3c", 
           "#fc4e2a", "#e31a1c", "#bd0026", "#800026", "black")

ggplot(data, aes(Patient, IP, fill= ifelse(IP != 20, IP, NA))) +
  geom_col() +
  scale_fill_gradientn(colours = IPcol, na.value = "white")+ 
  coord_flip()+
  scale_y_time()

如果要使用困难的选项,则必须编写一个调色板函数,该函数可在0-1范围内的值上工作,并将重新缩放的20设置为白色:

If you want the difficult option, you'd have to write a palette function that works on values ranged 0-1 and set the rescaled 20 to white:

# Define an area to set to white
target <- (c(19.5, 20.5) - min(data$IP)) / (max(data$IP) - min(data$IP))

# Build a palette function
my_palette <- function(colours, values = NULL) {
  ramp <- scales::colour_ramp(colours)
  force(values)
  function(x) {
    # Decide what values to replace
    replace <- x > target[1] & x < target[2]
    if (length(x) == 0)
      return(character())
    if (!is.null(values)) {
      xs <- seq(0, 1, length.out = length(values))
      f <- stats::approxfun(values, xs)
      x <- f(x)
    }
    out <- ramp(x)
    # Actually replace values
    out[replace] <- "white"
    out
  }
}

现在您可以像这样绘制它:

And now you could plot it like thus:

ggplot(data, aes(Patient, IP, fill= IP)) +
  geom_col() +
  continuous_scale("fill", "my_pal", my_palette(IPcol), 
                   guide = guide_colourbar(nbin = 100))+ 
  coord_flip()+
  scale_y_time()

这篇关于使用scale_fill_gradientn将特定颜色分配给条形图中的确定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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