R 将geom_平铺图切换到Stat_Density_2D图时出错

R 将geom_平铺图切换到Stat_Density_2D图时出错,r,ggplot2,plot,heatmap,R,Ggplot2,Plot,Heatmap,我一直在尝试用以下数据制作热图 数据: 我首先用这段代码创建了一个geom_瓷砖热图,并成功运行 library(RODBC) library(ggplot2) con=odbcConnect('username',uid='userid', pwd = 'password') df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from pitches_sample where pitchcall='InPlay

我一直在尝试用以下数据制作热图

数据:

我首先用这段代码创建了一个geom_瓷砖热图,并成功运行

library(RODBC)
library(ggplot2)


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

df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from pitches_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)
)

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()

有人知道如何正确地修改geom_tile热图,使输出看起来更像stat_density_2D绘图吗?提前谢谢

我们可以设置一个新网格,并将您的数据插入到新网格中。这将使它看起来不那么矩形

library(dplyr)
library(ggplot2)
library(gstat)
library(sp)

new_map <- df %>% rename(x = s, y = h)
coordinates(new_map) <- ~x + y
grd <- expand.grid(x = seq(from = -3, to = 3, by = .1), y = seq(from = 0, to = 5, by = .1))
coordinates(grd) <- ~x + y 
gridded(grd) <- TRUE 
idw <- idw(formula = exitspeed ~ 1, locations = new_map, newdata = grd) 
idw.output <- as.data.frame(idw)

ggplot(kZone, aes(x,y)) + 
    geom_tile(data=idw.output, aes(x=x, y=y, fill=var1.pred)) +
    scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(10, "Spectral")), breaks = c(60, 70, 80, 90, 100), labels = c(60, 70, 80, 90, 100), limits = c(60,100))+
    geom_path(lwd=1.5, col="black") +
    labs(fill = "ExitSpeed")+
    coord_fixed()
库(dplyr)
图书馆(GG2)
图书馆(gstat)
图书馆(sp)
新映射%rename(x=s,y=h)

坐标(新地图)非常有效,谢谢!快速跟进问题。是否可以将标题从var1.pred更改为ExitSpeed,并将var1.pred的范围从60-100更改为10(60,70,80,90100)?我知道这可能看起来很简单,但我不会在您的代码中遵循您可以更改的地方,因为您第一次调用var1.pred变量是在ggplot行中。再次感谢!是的,我现在就更新我的答案。“var1.pred”是在idw()命令期间生成的。您可以使用
labs()
更改图例标题,也可以在
scale.*\u gradient()
调用中更改分隔符和标签。这太棒了!非常感谢你的帮助!非常感谢!
library(RODBC)
library(ggplot2)


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

df=sqlQuery(con,"select platelocheight, platelocside, exitspeed from pitches_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)
)

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

ggplot(kZone, aes(x,y)) +
  stat_density_2d(data=df, aes(x=s, y=h, fill=es),geom="polygon") +
  scale_fill_distiller(palette = "Spectral") +
  geom_path(lwd=1.5, col="black") +
  coord_fixed()
Error in FUN(X[[i]], ...) : object 'exitspeed' not found
library(dplyr)
library(ggplot2)
library(gstat)
library(sp)

new_map <- df %>% rename(x = s, y = h)
coordinates(new_map) <- ~x + y
grd <- expand.grid(x = seq(from = -3, to = 3, by = .1), y = seq(from = 0, to = 5, by = .1))
coordinates(grd) <- ~x + y 
gridded(grd) <- TRUE 
idw <- idw(formula = exitspeed ~ 1, locations = new_map, newdata = grd) 
idw.output <- as.data.frame(idw)

ggplot(kZone, aes(x,y)) + 
    geom_tile(data=idw.output, aes(x=x, y=y, fill=var1.pred)) +
    scale_fill_gradientn(colours = rev(RColorBrewer::brewer.pal(10, "Spectral")), breaks = c(60, 70, 80, 90, 100), labels = c(60, 70, 80, 90, 100), limits = c(60,100))+
    geom_path(lwd=1.5, col="black") +
    labs(fill = "ExitSpeed")+
    coord_fixed()