R中的R图像函数

2024-02-27

我正在使用 R 中附加的图像函数。为了速度,我更喜欢使用它而不是热图,因为我将它用于巨大的矩阵(~ 400000 x 400)。

我的函数中的问题是调色板的动态范围,在我的例子中它只有蓝色和黄色。我尝试了对 colorramp 线的多次更改,但没有一个给我带来所需的输出。

我尝试的最后一个颜色渐变选项是在 R 中使用一个很好的包,称为颜色渐变 https://i.stack.imgur.com/K2D4k.png,给出合理结果的是:

library("colorRamps")
ColorRamp = blue2green2red(400)
ColorLevels <- seq(min, max, length=length(ColorRamp))

然而,仍然不如 matlab 色带选项灵活。

I am not very familiar on how to make it look better and with more range, such as in the photo attached.enter image description here

请告诉我是否可以更改我的图像功能以使我的图像看起来像照片中的图像。

我用于绘制图像的 R 函数,其中 raster = TRUE 表示速度,如下所示:

# ----- Define a function for plotting a matrix ----- #
myImagePlot <- function(x, filename, ...){
  dev = "pdf"
  #filename = '/home/unix/dfernand/test.pdf'
  if(dev == "pdf") { pdf(filename, version = "1.4") } else{}
     min <- min(x)
     max <- max(x)
     yLabels <- rownames(x)
     xLabels <- colnames(x)
     title <-c()
  # check for additional function arguments
  if( length(list(...)) ){
    Lst <- list(...)
    if( !is.null(Lst$zlim) ){
       min <- Lst$zlim[1]
       max <- Lst$zlim[2]
    }
    if( !is.null(Lst$yLabels) ){
       yLabels <- c(Lst$yLabels)
    }
    if( !is.null(Lst$xLabels) ){
       xLabels <- c(Lst$xLabels)
    }
    if( !is.null(Lst$title) ){
       title <- Lst$title
    }
  }
# check for null values
if( is.null(xLabels) ){
   xLabels <- c(1:ncol(x))
}
if( is.null(yLabels) ){
   yLabels <- c(1:nrow(x))
}

layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(1,1))

 # Red and green range from 0 to 1 while Blue ranges from 1 to 0
 ColorRamp <- rgb( seq(0,1,length=256),  # Red
                   seq(0,1,length=256),  # Green
                   seq(1,0,length=256))  # Blue
 ColorLevels <- seq(min, max, length=length(ColorRamp))

 # Reverse Y axis
 reverse <- nrow(x) : 1
 yLabels <- yLabels[reverse]
 x <- x[reverse,]

 # Data Map
 par(mar = c(3,5,2.5,2))
 image(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="",
 ylab="", axes=FALSE, zlim=c(min,max), useRaster=TRUE)
 if( !is.null(title) ){
    title(main=title)
 }
# Here we define the axis, left of the plot, clustering trees....
#axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.7)
# axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1,
# cex.axis=0.7)

 # Color Scale (right side of the image plot)
 par(mar = c(3,2.5,2.5,2))
 image(1, ColorLevels,
      matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1),
      col=ColorRamp,
      xlab="",ylab="",
      xaxt="n", useRaster=TRUE)

 layout(1)
  if( dev == "pdf") {
    dev.off() }
}
# ----- END plot function ----- #

您可以定义偏差colorRampPalette。我还调整了该函数来定义颜色之间的步数color.palette:

#This is a wrapper function for colorRampPalette. It allows for the
#definition of the number of intermediate colors between the main colors.
#Using this option one can stretch out colors that should predominate
#the palette spectrum. Additional arguments of colorRampPalette can also
#be added regarding the type and bias of the subsequent interpolation.
color.palette <- function(steps, n.steps.between=NULL, ...){

 if(is.null(n.steps.between)) n.steps.between <- rep(0, (length(steps)-1))
 if(length(n.steps.between) != length(steps)-1) stop("Must have one less n.steps.between value than steps")

 fill.steps <- cumsum(rep(1, length(steps))+c(0,n.steps.between))
 RGB <- matrix(NA, nrow=3, ncol=fill.steps[length(fill.steps)])
 RGB[,fill.steps] <- col2rgb(steps)

 for(i in which(n.steps.between>0)){
  col.start=RGB[,fill.steps[i]]
  col.end=RGB[,fill.steps[i+1]]
  for(j in seq(3)){
   vals <- seq(col.start[j], col.end[j], length.out=n.steps.between[i]+2)[2:(2+n.steps.between[i]-1)]  
   RGB[j,(fill.steps[i]+1):(fill.steps[i+1]-1)] <- vals
  }
 }

     new.steps <- rgb(RGB[1,], RGB[2,], RGB[3,], maxColorValue = 255)
 pal <- colorRampPalette(new.steps, ...)
 return(pal)
}

这是两者的示例(我已经压缩了青色和黄色之间的步数):

Z <- t(as.matrix(1:100))

pal.1 <- colorRampPalette(c("blue", "cyan", "yellow", "red"), bias=1)
pal.2 <- colorRampPalette(c("blue", "cyan", "yellow", "red"), bias=3)
pal.3 <- color.palette(c("blue", "cyan", "yellow", "red"), n.steps.between=c(10,1,10))

    x11()
par(mfcol=c(1,3))
image(Z, col=pal.1(100))
image(Z, col=pal.2(100))
image(Z, col=pal.3(100))

另外,如果你有兴趣,我写了一个函数 http://menugget.blogspot.de/2011/08/adding-scale-to-image-plot.html绘制色标并使用与以下相同的参数image。如果您正确设置了绘图布局,这也将是绘制矩阵和相应色标的快速方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R中的R图像函数 的相关文章

  • 更改 R 中 ggplot geom_polygon 的颜色方案

    我正在使用地图库和 ggplot 的 geom polygon 创建地图 我只是想将默认的蓝色 红色 紫色配色方案更改为其他颜色 我对 ggplot 非常陌生 所以如果我没有使用正确的数据类型 请原谅 我使用的数据如下所示 gt head
  • 在Python中调整图像大小

    我有一张尺寸为 288 352 的图像 我想将其大小调整为 160 240 我尝试了以下代码 im imread abc png img im resize 160 240 Image ANTIALIAS 但它给出了一个错误TypeErro
  • 条件和分组 mutate dplyr

    假设我有以下每个抽屉库存增加的数据 gt socks year drawer nbr sock total 1990 1 2 1991 1 2 1990 2 3 1991 2 4 1990 3 2 1991 3 1 我想要一个二进制变量来标
  • 在闪亮的数据表中为每个单元格显示工具提示或弹出窗口?

    有没有什么方法可以为 r闪亮数据表中的每个单元格获取工具提示 有很多方法可以获取悬停行或列 但我找不到一种方法来获取行和列索引并为每个单元格显示不同的悬停工具提示 任何人都可以修改以下代码吗 library shiny library DT
  • 32 位应用程序的特征最大矩阵大小

    所以 我正在寻找Eigen http eigen tuxfamily org index php title Main Page当我尝试声明大于 10000x10000 的矩阵时 包崩溃 我需要声明一个像这样的矩阵 可靠地大约有 13000
  • ggplot2:带有 geom_line 的 x 轴因子不起作用

    我想要一个线图 其中value绘制为函数expt每级一行var 这是我的数据 lines lt expt var value 1 none p 0 183065327746799 2 none p 0 254234138384241 3 n
  • R:改变堆积条形图的颜色

    library ggplot2 df2 lt data frame supp rep c VC OJ each 3 dose rep c D0 5 D1 D2 2 len c 6 8 15 33 4 2 10 29 5 head df2 g
  • 使用data.table进行聚合

    经过 SO 用户的多次建议后 我终于尝试将我的代码转换为使用data table library data table DT lt data table plate paste0 plate rep 1 2 each 5 id rep c
  • 将 Excel 文件读入 R 并锁定单元格

    我有一个 Excel 电子表格要读入 R 它受密码保护并锁定了单元格 我可以使用 excel link 导入受密码保护的文件 但我不知道如何解锁 取消保护单元格 excel link 给了我这个错误 gt
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d
  • rpart 决策树中的 rel 误差和 x 误差有什么区别? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个来自 UCI 机器学习数据库的纯分类数据框https archive ics uci edu ml datasets Diabet
  • ggplot 的每个方面都有不同的 `geom_hline()`

    这个问题在这里已经有答案了 library tidyverse ggplot mpg aes cty hwy geom point facet grid year fl geom hline yintercept mean mpg hwy
  • Python Matplotlib 箱线图颜色

    我正在尝试使用 Matplotlib 制作两组箱线图 我希望每组箱线图 以及点和晶须 以不同的颜色填充 所以基本上情节上会有两种颜色 我的代码如下 如果您能帮助将这些图绘制成彩色 那就太好了 d0 and d1是每个数据列表的列表 我想要一
  • tidyverse - 将命名向量转换为 data.frame/tibble 的首选方法

    使用tidyverse我经常面临将命名向量转换为向量的挑战data frame tibble列是向量的名称 执行此操作的首选 tidyverse 方式是什么 编辑 这与 this https github com hadley dplyr
  • 准确地从屏幕上的像素获取颜色并转换其颜色空间

    我需要从屏幕上的像素获取颜色并转换其颜色空间 我遇到的问题是 将值与数字色度计应用程序进行比较时 颜色值不相同 create a 1x1 image at the mouse position if let image CGImage CG
  • 通过消除嵌套的 for 循环来改进此代码

    R 包corrplot除其他内容外 还包含这个漂亮的功能 cor mtest lt function mat conf level 0 95 mat lt as matrix mat n lt ncol mat p mat lt lowCI
  • autoplot.microbenchmark 实际绘制了什么?

    根据文档 microbenchmark autoplot 使用 ggplot2 生成更清晰的微基准计时图 凉爽的 让我们尝试一下示例代码 library ggplot2 tm lt microbenchmark rchisq 100 0 r
  • Linux 中的 R 有哪些可用的 IDE? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 Linux 中的 R 有哪些好的 IDE 我尝试过 Rcmdr 和 Eclipse 但似乎都不具有与 Windows 中的 Tinn R
  • C 语言中的 Alpha 混合 2 RGBA 颜色[重复]

    这个问题在这里已经有答案了 可能的重复 如何快速进行阿尔法混合 https stackoverflow com questions 1102692 how to do alpha blend fast 对 2 个 RGBA 整数 颜色进行
  • 根据列中的部分字符串匹配选择数据框行

    我想根据列中字符串的部分匹配从数据框中选择行 例如列 x 包含字符串 hsa 使用sqldf if它有一个like语法 我会做类似的事情 select from lt gt where x like hsa 很遗憾 sqldf不支持该语法

随机推荐

  • 如何在 EL JSF 中使用方括号

    我见过有人在 JSF 中使用方括号 我不确定我是否正确理解它的用法 所以也许 JSF 大师可以帮助我理解它 1 假设我有这个 bean x x是一个二维数组 x 如何显示x 0 使用EL 我想在这种情况下我需要使用方括号 我想我用 bean
  • 当变量更改时收到通知

    有什么方法可以获取变量何时更改吗 如果是这样 我怎样才能实现这一目标 官方 的方法是INotifyPropertyChanged 例如 当控件绑定到的数据对象更新时 UI Windows 窗体 WPF 使用它来自动刷新控件 public c
  • jquery ui selectmenu滚动条不起作用

    我使用 jquery selectmenu 插件 我已经初始化选择 select selectmenu width 100 maxHeight 300 style dropdown 我有很多选项 这会导致出现默认浏览器滚动条 但我无法使用它
  • 将数据迁移到应用程序组会禁用 iCloud 同步

    我正在向现有应用程序添加 今日扩展 我添加了一个应用程序组并使用了这个post https stackoverflow com questions 52191523 ios 11 how to migrate existing core d
  • 在geom_sf中填充连续颜色

    我正在运行代码来在 R 中创建地图 library tidyverse library ggplot2 library eurostat library janitor library sf eugd lt eurostat geodata
  • 如何使用 Appcompat v7 21、工具栏和 DrawerLayout 将汉堡动画制作为箭头

    我将 android support v7 widget Toolbar 与 android support v4 widget DrawerLayout 一起使用 它工作正常 当导航抽屉关闭时显示汉堡图标 当抽屉打开时显示箭头图标 我想在
  • 有没有办法按 ASC 对 elasticsearch _score 进行排序?

    我有一个包含 574 279 238 个文档的索引 ES 因此 当我查询数据库时 我必须管理大量结果 有没有办法根据 ASC 排序的 score 获得结果 我希望看到 ES 给我的 X 个 最糟糕 结果 即使我有超过 100 万个结果 排序
  • 在 MongoDB 中保存点分字段[重复]

    这个问题在这里已经有答案了 我正在尝试将 JSON API 的结果保存到 MongoDB 集合 我尝试保存的 JSON 数据具有以下结构 compatibility 2 7 1 2 2 6 100 1 1 2 8 3 2 2 6 100 2
  • 在 Android Studio 中使用 gradle 2.4

    我想在 Android Studio 中使用最新版本的 Gradle 如何检查使用的是哪个版本 以及如何升级到最新版本 如果支持 我想升级 因为我读到最新版本的 Gradle 缩短了构建时间 You can check current Gr
  • 为什么使用 dtype np.int64 的操作比使用 np.int16 的相同操作慢得多?

    这就是我的意思 a是 1 000 000 的向量np int64元素 b是 1 000 000 的向量np int16要素 In 19 a np random randint 100 size 10 6 dtype int64 In 20
  • 如何在 Pandas 中选择仅包含正值或负值的行

    我有以下 DF df pd DataFrame x 1 2 3 1 2 3 y 1 3 2 4 3 2 z 1 1 5 2 1 1 or x y z 0 1 1 1 1 2 3 1 2 3 2 5 3 1 4 2 4 2 3 1 5 3 2
  • 如何创建带圆角的用户控件?

    我正在尝试拥有一个具有圆角的用户控件 它没有固定的大小 但宽度通常不会超过 120 像素 我需要用户控件及其内容 标签和表格 具有圆形边缘并且看起来像圆形盒子 我已经使用过这段代码 DllImport Gdi32 dll EntryPoin
  • 使用通配符扩展来回显 zsh 中的所有变量

    对于以相同模式开头的多个变量 可以使用通配符来回显所有匹配的模式吗 when zzz1 test1 zzz A test2 zzza test3 匹配以 zzz 开头的所有变量的最佳方法是什么 哪里像echo zzz or for i in
  • 规范覆盖和最小覆盖之间的区别

    我知道如何计算最低保障 确保每个功能依赖项在 RHS 上只有一个属性 通过计算每个属性的闭包来删除无关 冗余的 LHS 属性 检查所有 FD 看看是否可以删除任何 FD 再次通过计算闭包 规范 封面只是同一事物的另一个词吗 规范封面 允许
  • 在 Talend 中加载一组文件的策略

    我想知道在 Talend 中解决以下问题的最佳策略是什么 我需要从存储在名称类似于 SAMPLE1 DAT SAMPLE2 DAT SAMPLEX DAT 的目录中的一组分隔文件中加载数据 目标将是 MySQL 数据库中的一个表 我必须立即
  • 内联复选框ajax修改数字

    我有一个像这样的 html 例如 li li
  • Keycloak/Wildfly 如何将所有控制台日志配置为 JSON 格式

    我正在使用官方 Keycloak 图像并尝试为控制台日志设置 JSON 格式 如下所示 启动 cli embed server server config standalone ha xml std out echo subsystem l
  • 导轨 3;活动记录;在哪里;数据库中两列之间的 NOT EQUAL 条件比较

    有没有某种方法可以使用比较数据库中的两列where 比如说 我有两列user这告诉我们 出生城市 最喜欢的城市 我想要一个具有不同于 city of birth 的 favourite city 的用户列表 我希望这样的事情能够奏效 use
  • 模糊图像的阈值 - 第 2 部分

    如何对这个模糊图像进行阈值处理以使数字尽可能清晰 In 以前的帖子 https stackoverflow com questions 13391073 adaptive threshold of blurry image 我尝试对模糊图像
  • R中的R图像函数

    我正在使用 R 中附加的图像函数 为了速度 我更喜欢使用它而不是热图 因为我将它用于巨大的矩阵 400000 x 400 我的函数中的问题是调色板的动态范围 在我的例子中它只有蓝色和黄色 我尝试了对 colorramp 线的多次更改 但没有