R 将第三个变量添加到stat_density_2d绘图
下面是我在R中创建的统计密度2d图的代码和数据集R 将第三个变量添加到stat_density_2d绘图,r,ggplot2,stat-density2d,R,Ggplot2,Stat Density2d,下面是我在R中创建的统计密度2d图的代码和数据集 library(ggplot2) 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, botKz
library(ggplot2)
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),geom="polygon") +
scale_fill_distiller(palette = "Spectral") +
geom_path(lwd=1.5, col="black") +
coord_fixed()
代码成功运行,但是,我想添加第三个fill变量(exitspeed),使其看起来更像热图。我尝试将'fill=es'添加到stat_density_2d行,但代码要么忽略'fill=es'行,要么说找不到变量es
下面是我的代码正在绘制的图片,以及我希望绘制的样子
当前代码:
我想要的是:
注意:我仍然希望在图表的右侧有一个刻度
有人知道如何正确地将第三个变量添加到stat_density_2d绘图中吗?我也愿意使用其他绘图/包来构建此热图。提前谢谢 您的图表有两个问题:
-首先,如注释所示的不同比例(单位)。这使得不可能像我在评论中建议的那样,简单地为exitspeed创建第二个
stat\u density
。此外,填充=…密度。。在这种情况下不起作用,因为我们讨论的是另一个变量。-第二,粗略的x/y值(见下文) 粗x/y坐标的问题是,插值不是很平滑。人们可以改变插值参数,但我还不知道怎么做@JasonAizkalns向这个方向提问,但不幸的是,目前还没有答案 不过,更精确的x/y坐标肯定会有所帮助。那么为什么不半人工预测呢 您基本上想要的是为密度等高线图内的每个x/y坐标指定一个退出速度值!(虽然我个人认为这可能没有真正意义,因为这些事情不一定相关。) 现在-在下面,我将预测原始图中密度等值线最大多边形(!)内随机采样的x/y值。让我们看看:
require(fields)
require(dplyr)
require(sp)
p <- ggplot_build(ggplot() +
stat_density_2d(data = df, aes(x = platelocside, y = platelocheight)) +
lims(x = c(-2,2), y = c(1,5)))$data[[1]] %>%
filter(level == min(level))
#this one is a bit tricky: I increased the limits of the axis of the plot in order to get an 'entire' polygon. I then filtered the rows of the largest polygon (minimum level)
poly_object <- Polygon(cbind(p$x, p$y)) #create Spatial object from polygon coordinates
random_points <- apply(coordinates(spsample(poly_object,10000, type = 'random')),2, round, digits = 1) #(coordinates() pulls out x/y coordinates, I rounded because this unifies the coordinates, and then I sampled random points within this polygon)
tps_x <- cbind(df$platelocside, df$platelocheight) #matrix of independent values for Tps() function
tps_Y <- df$exitspeed #dependent value for model prediction
fit <- Tps(tps_x, tps_Y)
predictedVal <- predict(fit, random_points) #predicting the exitspeed-values
ggplot() +
geom_raster(aes(x = random_points[,'x'], y = random_points[,'y'], fill = predictedVal), interpolate = TRUE)+
stat_density_2d(data = df, aes(x = platelocside, y = platelocheight)) +
geom_path(data = kZone, aes(x,y))
require(字段)
需要(dplyr)
需要(sp)
p%
过滤器(级别==最小(级别))
#这个有点棘手:为了得到一个“完整”的多边形,我增加了绘图轴的限制。然后我过滤了最大多边形的行(最小级别)
看看这个答案,可能会有帮助。在看了一眼之后——实际上,这对我来说并不完全有意义——解决方案只是制作第二个stat\u density
层。但是你的退出速度
和s
——它们有非常不同的尺度!那些是不同的单位吗?您需要先从一个转换到另一个,以使重新绘制成为可能。感谢Tjebo的回复!是的,它们是不同的单位s
是球场的水平位置,exitspeed
是击球的力度(如果你不熟悉这项运动,这是一个棒球项目)。你把一个转换成另一个是什么意思?谢谢你的回复Tjebo!这非常有帮助。是的,平滑度绝对是个问题。我已经能够用插值生成一些网格类型的图,但是我仍然在努力找到一个解决方案来解决你提到的平滑填充问题。再次感谢@NateWalker看看我的最新答案,应该很接近。这太棒了!谢谢你的帮助,杰博!
ggplot(kZone, aes(x,y)) +
stat_density_2d(data=df, aes(x = s, y = h)) +
geom_raster(data = df, aes(x = s, y = h, fill = exitspeed), interpolate = TRUE)
#doesn't do the job, as the grid is to coarse
require(fields)
require(dplyr)
require(sp)
p <- ggplot_build(ggplot() +
stat_density_2d(data = df, aes(x = platelocside, y = platelocheight)) +
lims(x = c(-2,2), y = c(1,5)))$data[[1]] %>%
filter(level == min(level))
#this one is a bit tricky: I increased the limits of the axis of the plot in order to get an 'entire' polygon. I then filtered the rows of the largest polygon (minimum level)
poly_object <- Polygon(cbind(p$x, p$y)) #create Spatial object from polygon coordinates
random_points <- apply(coordinates(spsample(poly_object,10000, type = 'random')),2, round, digits = 1) #(coordinates() pulls out x/y coordinates, I rounded because this unifies the coordinates, and then I sampled random points within this polygon)
tps_x <- cbind(df$platelocside, df$platelocheight) #matrix of independent values for Tps() function
tps_Y <- df$exitspeed #dependent value for model prediction
fit <- Tps(tps_x, tps_Y)
predictedVal <- predict(fit, random_points) #predicting the exitspeed-values
ggplot() +
geom_raster(aes(x = random_points[,'x'], y = random_points[,'y'], fill = predictedVal), interpolate = TRUE)+
stat_density_2d(data = df, aes(x = platelocside, y = platelocheight)) +
geom_path(data = kZone, aes(x,y))