r - 使用两个数据框创建 geom_tile 图

标签 r ggplot2 heatmap

下面是尝试使用 geom_tile 创建热图的 R 代码。在我的图中,我需要绘制一个盒子的轮廓和一对单独的 x//y 坐标(代码中标题为 Platelocside 和 Platelocheight),它们还具有填充(代码中标题为 exitspeed)以完成热图。这是我想要绘制的数据框的当前结构(在代码中标记为“df”)。

structure(list(platelocheight = c(2.594, 3.803, 3.254, 3.599, 
3.617, 3.297, 2.093, 3.611, 2.842, 3.316, 2.872, 3.228, 3.633, 
4.28, 3.309, 2.8, 2.632, 3.754, 2.207, 3.604, 3.443, 2.188, 3.452, 
2.553, 3.382, 3.067, 2.986, 2.785, 2.567, 3.804), platelocside = c(0.059, 
-1.596, -0.65, -0.782, -0.301, -0.104, 0.057, -0.807, 0.003, 
1.661, 0.088, -0.32, -1.115, -0.146, -0.364, -0.952, 0.254, 0.109, 
-0.671, -0.803, -0.212, -0.069, -0.09, -0.472, 0.434, 0.337, 
0.723, 0.508, -0.197, -0.635), exitspeed = c(69.891, 73.352, 
83.942, 85.67, 79.454, 85.277, 81.078, 73.573, 77.272, 59.263, 
97.343, 91.436, 76.264, 83.479, 47.576, 84.13, 60.475, 61.093, 
84.54, 69.959, 88.729, 88.019, 82.18, 83.684, 86.296, 90.605, 
79.945, 59.899, 62.522, 77.75)), .Names = c("platelocheight", 
"platelocside", "exitspeed"), row.names = c(NA, 30L), class = "data.frame")
> 

当我运行代码时,我能够在输出中获得框的轮廓,但其他数据框(标题 df)未绘制。有谁知道谁使用 geom_tile 能够绘制两个单独的数据框?提前致谢!

library(RODBC)
library(ggplot2)


con=odbcConnect('ID',uid='username', pwd = 'password')

df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from tm_sample where pitchcall='InPlay' 
and exitspeed is not null")

topKzone <- 3.5
botKzone <- 1.6
inKzone <- -0.95
outKzone <- 0.95
kZone <- data.frame(
  x=c(inKzone, inKzone, outKzone, outKzone, inKzone),
  y=c(botKzone, topKzone, topKzone, botKzone, botKzone)
)

ggplot(kZone, aes(x,y)) +
  geom_tile(data=df, aes(x=platelocside, y=platelocheight, fill= exitspeed)) +
  scale_fill_distiller(palette = "Spectral") +
  geom_path(lwd=1.5, col="black") +
  coord_fixed() 

最佳答案

问题不在于使用两个数据帧,而在于 geom_tile() 本身的实现。

如果将 geom_tile 替换为不同的几何图形,例如 geom_pointgeom_hex,您会看到绘图已渲染完美:

library(ggplot2)
ggplot(kZone, aes(x,y)) +
  geom_hex(data=df, aes(x=platelocside, y=platelocheight, col=exitspeed)) +
  scale_fill_distiller(palette = "Spectral") +
  geom_path(lwd=1.5, col="black") +
  coord_fixed() 

产生这个: enter image description here

了解geom_tile

geom_tile 对于您的数据来说不是一个好的选择,因为您使用的是连续的 x 和 y 比例,与类似热图的数据相比,这使得它对于散点图之类的数据来说是更可行的选择形象的。

你可以看一个例子:

ggplot(mtcars, aes(x=as.factor(gear), y=as.factor(cyl), fill=hp))+
    geom_tile()

enter image description here

与对两个连续变量调用它时相比:

ggplot(mtcars, aes(x=wt, y=mpg, fill=hp))+
    geom_tile()

您将得到的图 block 实际上非常小,看起来就像没有绘制任何内容。

回到您的问题,您使用的 df 具有 platelocsideplatelocheight 均为数字连续变量。这使得 geom_hex 成为一个不太理想的选择。如果您坚持使用geom_tile,那么我会使用以下两种解决方案之一:

解决方案1

使用 col 而不是 fill 来获取点而不是图 block (因为 xy 不是因子变量)

library(ggplot2)
ggplot(kZone, aes(x,y)) +
  geom_tile(data=df, aes(x=platelocside, y=platelocheight, col=exitspeed), size=4) +
  scale_fill_distiller(palette = "Spectral") +
  geom_path(lwd=1.5, col="black") +
  coord_fixed() 

解决方案2

使xy成为因子变量:

df$h <- round(df$platelocheight)
df$s <- round(df$platelocside)

ggplot(kZone, aes(x,y)) +
  geom_tile(data=df, aes(x=s, y=h, fill=exitspeed)) +
  scale_fill_distiller(palette = "Spectral") +
  geom_path(lwd=1.5, col="black") +
  coord_fixed() 

enter image description here

关于r - 使用两个数据框创建 geom_tile 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51664480/

相关文章:

根据列表名称重命名列表列表的第一列

datetime - R:计算2个数字时间之间耗时,例如944和1733是469分钟

r - 在 ggplot2 中按因子级别设置轴中断

r - ggplot2:在图形面板集上覆盖控制组线

javascript - 带气泡的 HighCharts 热图

r - ggmap R 中带有数量的热图

r - 如何加快 tidyr up 中的函数速度

string - 数据框中的 R 变量字符串替换

r - 为什么scale_linetype_manual()会巧妙地改变线型的美感?

r - 热图 ggplot2 色带(scale_fill_gradient)