我正在使用 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)
输出:
我遇到的问题是,我不确定如何添加图例/色标来描述不同色调之间数字项的相对差异。例如,如果没有某种图例或色标,就无法判断上图中最深的蓝色的密度是最浅的蓝色的 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)
您可以摆弄 image.plot 来获得您想要的内容,然后查看 ?bkde2D
和 smoothScatter
的 transformation
参数来获得了解颜色代表什么。
关于使用 smoothScatter 生成的颜色密度散点图的 R 图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14271584/