如何在ggplot中绘制纵坐标不规则数据的热图?
How to plot a heat map with irregular data in ordinates in ggplot?
假设我想绘制以下数据:
# First set of X coordinates
x <- seq(0, 10, by = 0.2)
# Angles from 0 to 90 degrees
angles <- seq(0, 90, length.out = 10)
# Convert to radian
angles <- deg2rad(angles)
# Create an empty data frame
my.df <- data.frame()
# For each angle, populate the data frame
for (theta in angles) {
y <- sin(x + theta)
tmp <- data.frame(x = x, y = y, theta = as.factor(theta))
my.df <- rbind(my.df, tmp)
}
x1 <- seq(0, 12, by = 0.3)
y1 <- sin(x1 - 0.5)
tmp <- data.frame(x = x1, y = y1, theta = as.factor(-0.5))
my.df <- rbind(my.df, tmp)
ggplot(my.df, aes(x, y, color = theta)) + geom_line()
这给了我一个很好的情节:
现在我想根据这个数据集绘制热图。有教程 here and there 使用 geom_tile
来完成。
所以,让我们试试:
# Convert the angle values from factors to numerics
my.df$theta <- as.numeric(levels(my.df$theta))[my.df$theta]
ggplot(my.df, aes(theta, x)) + geom_tile(aes(fill = y)) + scale_fill_gradient(low = "blue", high = "red")
这不行,原因是我的x坐标没有相同的步长:
x <- seq(0, 10, by = 0.2)
对比 x1 <- seq(0, 12, by = 0.3)
但是只要我使用相同的步骤 x1 <- seq(0, 12, by = 0.2)
,它就会起作用:
我在现实生活中,我的数据集没有规则间隔(这些是实验数据),但我仍然需要将它们显示为热图。我该怎么办?
您可以使用 akima
将函数插入到适合热图绘图的形式。
library(akima)
library(ggplot2)
my.df.interp <- interp(x = my.df$theta, y = my.df$x, z = my.df$y, nx = 30, ny = 30)
my.df.interp.xyz <- as.data.frame(interp2xyz(my.df.interp))
names(my.df.interp.xyz) <- c("theta", "x", "y")
ggplot(my.df.interp.xyz, aes(x = theta, y = x, fill = y)) + geom_tile() +
scale_fill_gradient(low = "blue", high = "red")
如果您想使用不同的分辨率,您可以将 nx
和 ny
参数更改为 interp
。
仅使用 ggplot2
的另一种方法是使用 stat_summary_2d
.
library(ggplot2)
ggplot(my.df, aes(x = theta, y = x, z = y)) + stat_summary_2d(binwidth = 0.3) +
scale_fill_gradient(low = "blue", high = "red")
假设我想绘制以下数据:
# First set of X coordinates
x <- seq(0, 10, by = 0.2)
# Angles from 0 to 90 degrees
angles <- seq(0, 90, length.out = 10)
# Convert to radian
angles <- deg2rad(angles)
# Create an empty data frame
my.df <- data.frame()
# For each angle, populate the data frame
for (theta in angles) {
y <- sin(x + theta)
tmp <- data.frame(x = x, y = y, theta = as.factor(theta))
my.df <- rbind(my.df, tmp)
}
x1 <- seq(0, 12, by = 0.3)
y1 <- sin(x1 - 0.5)
tmp <- data.frame(x = x1, y = y1, theta = as.factor(-0.5))
my.df <- rbind(my.df, tmp)
ggplot(my.df, aes(x, y, color = theta)) + geom_line()
这给了我一个很好的情节:
现在我想根据这个数据集绘制热图。有教程 here and there 使用 geom_tile
来完成。
所以,让我们试试:
# Convert the angle values from factors to numerics
my.df$theta <- as.numeric(levels(my.df$theta))[my.df$theta]
ggplot(my.df, aes(theta, x)) + geom_tile(aes(fill = y)) + scale_fill_gradient(low = "blue", high = "red")
这不行,原因是我的x坐标没有相同的步长:
x <- seq(0, 10, by = 0.2)
对比 x1 <- seq(0, 12, by = 0.3)
但是只要我使用相同的步骤 x1 <- seq(0, 12, by = 0.2)
,它就会起作用:
我在现实生活中,我的数据集没有规则间隔(这些是实验数据),但我仍然需要将它们显示为热图。我该怎么办?
您可以使用 akima
将函数插入到适合热图绘图的形式。
library(akima)
library(ggplot2)
my.df.interp <- interp(x = my.df$theta, y = my.df$x, z = my.df$y, nx = 30, ny = 30)
my.df.interp.xyz <- as.data.frame(interp2xyz(my.df.interp))
names(my.df.interp.xyz) <- c("theta", "x", "y")
ggplot(my.df.interp.xyz, aes(x = theta, y = x, fill = y)) + geom_tile() +
scale_fill_gradient(low = "blue", high = "red")
如果您想使用不同的分辨率,您可以将 nx
和 ny
参数更改为 interp
。
仅使用 ggplot2
的另一种方法是使用 stat_summary_2d
.
library(ggplot2)
ggplot(my.df, aes(x = theta, y = x, z = y)) + stat_summary_2d(binwidth = 0.3) +
scale_fill_gradient(low = "blue", high = "red")