R 具有对数比例尺的geom_光栅插值

R 具有对数比例尺的geom_光栅插值,r,plot,ggplot2,raster,logarithm,R,Plot,Ggplot2,Raster,Logarithm,我有点困在用对数比例绘制光栅上。考虑这个情节,例如: ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) dat <- data.frame(x = rep(1:10, 10), y = unlist(lapply(1:10, function(i) rep(i, 10))), z = faithfuld$d

我有点困在用对数比例绘制光栅上。考虑这个情节,例如:

ggplot(faithfuld, aes(waiting, eruptions)) +
 geom_raster(aes(fill = density))
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()

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

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

这根本不起作用:

 ggplot(faithfuld, aes(waiting, (eruptions))) +
   geom_raster(aes(fill = density)) + 
   coord_trans(x="log10")
错误:geom_光栅仅适用于笛卡尔坐标

是否有任何选项可用于将对数标尺与光栅一起使用

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

ggplot(faithfuld, aes(waiting, eruptions)) +
 geom_raster(aes(fill = density))
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()

dat数据集
faithfuld
已经有一个密度列,它是等待和喷发的2D密度估计值。您可以发现数据集中的喷发和等待是网格中的点。使用
geom\u光栅时,它不会为您计算密度。相反,它根据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)    

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

由于
geom_光栅
使用相同大小的平铺,您可能必须使用
geom_平铺
geom_rect
来创建绘图。我的想法是计算每个磁贴的大小(宽度),并调整每个磁贴的
xmin
xmax
,以填补空白

 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)

dat谢谢,在我的实际用例中,我真的需要使用第三个变量作为密度,就像在
faithfuld
数据集中一样,所以你的建议并不能解决我的问题。你还有其他想法吗?我不知道你说的“真的需要使用第三个变量作为密度”是什么意思。你有办法计算密度吗?另一种方法是检索
ggplot
用于构建绘图的数据。您可以将绘图保存为对象,如
g
,并通过
ggplot\u build(g)$data
检索数据。数据包含密度。或者你能编辑问题并提供实际用例的子集吗?我的意思是,我的数据集中有三列,z列不是从x或y列计算出来的。我有另一种计算z值的方法,与绘图函数无关。我想使用z列作为光栅的颜色值。谢谢你的更新,这解决了我的问题。我已经编辑了您的答案,使您的解决方案更加通用(并且适合我的实际用例)。