r - 带对数刻度的 geom_raster 插值

标签 r plot ggplot2 raster logarithm

我在用对数刻度绘制栅格时有点卡住了。以这个图为例:

ggplot(faithfuld, aes(waiting, eruptions)) +
 geom_raster(aes(fill = density))

enter image description here

但是如何在这个几何图形中使用对数刻度呢?通常的方法都不是很令人满意:

 ggplot(faithfuld, aes(waiting, log10(eruptions))) +
   geom_raster(aes(fill = density))

enter image description here

 ggplot(faithfuld, aes(waiting, (eruptions))) +
   geom_raster(aes(fill = density)) + 
   scale_y_log10()

enter image description here

这根本不起作用:

 ggplot(faithfuld, aes(waiting, (eruptions))) +
   geom_raster(aes(fill = density)) + 
   coord_trans(x="log10")

错误:geom_raster 仅适用于笛卡尔坐标

是否有任何选项可以将对数刻度与栅格一起使用?

准确地说,我有三列数据。 z 值是我想用来为栅格着色的值,它不是根据 x 和 y 值计算的。因此,我需要向 ggplot 函数提供所有三列。例如:

dat <- data.frame(x = rep(1:10, 10), 
                  y = unlist(lapply(1:10, function(i) rep(i, 10))), 
                  z = faithfuld$density[1:100])

ggplot(dat, aes(x = log(x), y = y, fill = z)) +
  geom_raster()

enter image description here

我该怎么做才能消除光栅中的这些间隙?

请注意,这个问题与这两个问题相关:

我一直在保留 R 代码的更新要点,其中结合了这些问题的答案中的详细信息(要点中包含示例输出)。要点在这里:https://gist.github.com/benmarwick/9a54cbd325149a8ff405

最佳答案

数据集faithfuld已经有一列密度,它是等待和喷发的二维密度的估计。您可以发现数据集中的喷发和等待是网格中的点。当您使用geom_raster时,它不会为您计算密度。相反,它根据 x、y 坐标(在本例中为网格)绘制密度。因此,如果您仅对 y 应用对数变换,它将扭曲 y 之间的差异(最初它们是等距的),这就是您在图中看到空间的原因。我用点来可视化效果:

library(ggplot2)
library(gridExtra)

# Use point to visualize the effect of log on the dataset
g1 <- ggplot(faithfuld, aes(x=waiting, y=eruptions)) +
  geom_point(size=0.5)    

g2 <- ggplot(faithfuld, aes(x=waiting, y=log(eruptions))) +
  geom_point(size=0.5)    

grid.arrange(g1, g2, ncol=2)    

enter image description here

如果您确实想将 y 转换为对数尺度并生成密度图,则必须将 faithful 数据集与 geom_ Density_2d 结合使用。

# Use geom_density_2d
ggplot(faithful, aes(x=waiting, y=log(eruptions))) +
  geom_density_2d() +
  stat_density_2d(geom="raster", aes(fill=..density..),
                  contour=FALSE)

enter image description here

更新:使用geom_rect并提供自定义xmin、xmax、ymin、ymax值以适应对数刻度的空间。

由于 geom_raster 使用相同大小的图 block ,因此您可能必须使用 geom_tilegeom_rect 来创建绘图。我的想法是计算每个图 block 应该有多大(宽度),并调整每个图 block 的 xminxmax 以填补间隙。

 dat <- data.frame(x = rep(1:10, 10), 
                  y = unlist(lapply(1:10, function(i) rep(i, 10))), 
                  z = faithfuld$density[1:100])
library(ggplot2)
library(gridExtra)   

g <- ggplot(dat, aes(x = log(x), y = y, fill = z)) +
  geom_raster()   

# Replace the ymin and ymax
distance <- diff((unique(dat$x)))/2
upper <- (unique(dat$x)) + c(distance, distance[length(distance)])
lower <- (unique(dat$x)) - c(distance[1], distance) 

# Create xmin, xmax, ymin, ymax
dat$xmin <- dat$x - 0.5 # default of geom_raster is 0.5
dat$xmax <- dat$x + 0.5
dat$ymin <- unlist(lapply(lower, function(i) rep(i, rle(dat$y)$lengths[1])))
dat$ymax <- unlist(lapply(upper, function(i) rep(i, rle(dat$y)$lengths[1])))        

# You can also use geom_tile with the width argument
g2 <- ggplot(dat, aes(x=log(x), y=y, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=z)) +
  geom_rect() 

# show the plots     
grid.arrange(g, g2, ncol=2)

enter image description here

关于r - 带对数刻度的 geom_raster 插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866379/

相关文章:

python - 来自点云的 3D 凸包

R 正在绘制页面上的标签

html - 在 R 中自定义传单 map 图标

带有 .combine=rbindlist 的 R foreach

matlab - 向绘图上的数据点添加额外信息

r - 显示从大到小的散点图 R 的值

r - 如何使用 grid.arrange 对齐条形图?

按值对 geom_bar ggplot2 中的条形图重新排序

r - 首先按组总和对数据帧进行排序,然后按频率排序

r - 更改 R 中 lmtest 的零假设