使用 smoothScatter 生成的颜色密度散点图的 R 图例

标签 r

我正在使用 smoothScatter() 函数在 R 中生成颜色密度散点图。

示例:

## A largish data set
n <- 10000
x1  <- matrix(rnorm(n), ncol = 2)
x2  <- matrix(rnorm(n, mean = 3, sd = 1.5), ncol = 2)
x   <- rbind(x1, x2)
oldpar <- par(mfrow = c(2, 2))
smoothScatter(x, nrpoints = 0)

输出:

enter image description here

我遇到的问题是,我不确定如何添加图例/色标来描述不同色调之间数字项的相对差异。例如,如果没有某种图例或色标,就无法判断上图中最深的蓝色的密度是最浅的蓝色的 2 倍、10 倍还是 100 倍。 R 中是否有任何方法可以检索必要的信息来制作这样的比例,或者任何内置的东西可以自动生成这种性质的色标?

最佳答案

这是一个依赖于 fields::imageplot 的答案,并使用 par(mar) 来获得正确的边距

fudgeit <- function(){
  xm <- get('xm', envir = parent.frame(1))
  ym <- get('ym', envir = parent.frame(1))
  z  <- get('dens', envir = parent.frame(1))
  colramp <- get('colramp', parent.frame(1))
  fields::image.plot(xm,ym,z, col = colramp(256), legend.only = T, add =F)
}

par(mar = c(5,4,4,5) + .1)
smoothScatter(x, nrpoints = 0, postPlotHook = fudgeit)

enter image description here

您可以摆弄 image.plot 来获得您想要的内容,然后查看 ?bkde2DsmoothScattertransformation 参数来获得了解颜色代表什么。

关于使用 smoothScatter 生成的颜色密度散点图的 R 图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14271584/

相关文章:

r - 将聚类结果叠加到排序上

R 插入符号错误 : Something is wrong; all the Accuracy metric values are missing:

r - 使用 ggplot2 在 x 轴上的月份之间添加刻度

r - 如何在 map 上的特定国家着色

r - 根据正则表达式拆分data.table列

r - 将 for 循环方程中的最后一个变量存储到数组中

r - 使用 terra 将列添加到 shapefile?

重命名环境中的数据集

r - 仅向部分数据点添加标签

r - 在 Azure Databricks 上安装 rgdal 和 rgeos