geom_raster 对数刻度插值
geom_raster interpolation with log scale
我在绘制具有对数刻度的栅格时遇到了一些问题。例如考虑这个情节:
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density))
但是这个geom如何使用对数刻度呢? None常用的方法很满意:
ggplot(faithfuld, aes(waiting, log10(eruptions))) +
geom_raster(aes(fill = density))
ggplot(faithfuld, aes(waiting, (eruptions))) +
geom_raster(aes(fill = density)) +
scale_y_log10()
这根本不起作用:
ggplot(faithfuld, aes(waiting, (eruptions))) +
geom_raster(aes(fill = density)) +
coord_trans(x="log10")
Error: geom_raster only works with Cartesian coordinates
是否有将对数刻度用于栅格的选项?
准确的说,我有三列数据。 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()
我该怎么做才能消除光栅中的这些间隙?
注意这个问题与这两个有关:
我一直在更新 R 代码的要点,其中结合了这些问题的答案的详细信息(要点中包含示例输出)。要点在这里:https://gist.github.com/benmarwick/9a54cbd325149a8ff405
数据集faithfuld
已经有一个密度列,它是等待和喷发的二维密度的估计值。你可以发现数据集中的喷发和等待是网格中的点。当您使用 geom_raster
时,它不会为您计算密度。相反,它根据 x、y 坐标绘制密度,在这种情况下,是网格。因此,如果您只是对 y 应用对数变换,它会扭曲 y 之间的差异(最初它们是相等的 spaced),这就是为什么您会在图中看到 space 的原因。我用点来可视化效果:
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)
如果您真的想将 y 转换为对数刻度并生成密度图,则必须使用具有 geom_density_2d
的 faithful
数据集。
# 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)
更新:使用 geom_rect
并提供自定义 xmin、xmax、ymin、ymax 值以适应对数刻度的 spaces。
由于 geom_raster
使用相同大小的图块,您可能必须使用 geom_tile
或 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)
我在绘制具有对数刻度的栅格时遇到了一些问题。例如考虑这个情节:
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density))
但是这个geom如何使用对数刻度呢? None常用的方法很满意:
ggplot(faithfuld, aes(waiting, log10(eruptions))) +
geom_raster(aes(fill = density))
ggplot(faithfuld, aes(waiting, (eruptions))) +
geom_raster(aes(fill = density)) +
scale_y_log10()
这根本不起作用:
ggplot(faithfuld, aes(waiting, (eruptions))) +
geom_raster(aes(fill = density)) +
coord_trans(x="log10")
Error: geom_raster only works with Cartesian coordinates
是否有将对数刻度用于栅格的选项?
准确的说,我有三列数据。 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()
我该怎么做才能消除光栅中的这些间隙?
注意这个问题与这两个有关:
我一直在更新 R 代码的要点,其中结合了这些问题的答案的详细信息(要点中包含示例输出)。要点在这里:https://gist.github.com/benmarwick/9a54cbd325149a8ff405
数据集faithfuld
已经有一个密度列,它是等待和喷发的二维密度的估计值。你可以发现数据集中的喷发和等待是网格中的点。当您使用 geom_raster
时,它不会为您计算密度。相反,它根据 x、y 坐标绘制密度,在这种情况下,是网格。因此,如果您只是对 y 应用对数变换,它会扭曲 y 之间的差异(最初它们是相等的 spaced),这就是为什么您会在图中看到 space 的原因。我用点来可视化效果:
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)
如果您真的想将 y 转换为对数刻度并生成密度图,则必须使用具有 geom_density_2d
的 faithful
数据集。
# 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)
更新:使用 geom_rect
并提供自定义 xmin、xmax、ymin、ymax 值以适应对数刻度的 spaces。
由于 geom_raster
使用相同大小的图块,您可能必须使用 geom_tile
或 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)