本文介绍了将第三个变量添加到stat_density_2d图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在R中创建的stat_density_2d图的代码和数据集.

 库(ggplot2)topKzone<-3.5botKzone<-1.6墨水区<---0.95outKzone<-0.95kZone<-data.frame(x = c(inKzone,inKzone,outKzone,outKzone,inKzone),y = c(botKzone,topKzone,topKzone,botKzone,botKzone))df $ h<-回合(df $ platelocheight)df $ s<-回合(df $ platelocside)df $ es<-回合(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() 

数据:

  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、2207、3.604、3.443、2.188、3.452,2.553,3.382,3.067,2.986,2.785,2.567,3.804),板位= 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),退出速度= 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)).名称= c("platelocheight","platelocside","exitspeed"),row.names = c(NA,30L),class ="data.frame") 

代码成功运行,但是,我想添加第三个填充变量(exitspeed),使其看起来更像是一个热图.我曾尝试在stat_density_2d行中添加"fill = es",但代码要么忽略了"fill = es"行,要么说找不到变量es.

下面是我的代码现在正在绘制的图形以及我希望绘制的图形的图片.

当前代码:

我想要什么:

注意:我仍然希望在图形的右侧有一个比例.

有人知道如何在stat_density_2d图中正确添加第三个变量吗?我也愿意使用其他图/包来构建此热图.预先感谢!

解决方案

图形有两个问题:
-首先,如所评论的不同比例(单位).正如我在评论中所建议的那样,这使得不可能简单地为退出速度创建第二个 stat_density .同样,在这种情况下,fill = ..density ..将不起作用,因为我们正在谈论的是另一个变量.
-其次,粗略的x/y值(请参见下文).

  ggplot(kZone,aes(x,y))+stat_density_2d(data = df,aes(x = s,y = h))+geom_raster(数据= df,aes(x = s,y = h,fill =退出速度),插值= TRUE)#不做这项工作,因为网格太粗了 

x/y坐标较粗的问题是,插值不是很平滑.可以更改插值参数,但是我不知道该怎么做(至今).@JasonAizkalns朝这个方向

Below is code and a dataset of a stat_density_2d plot I created in R.

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

Data:

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

The code successfully runs, however, I would like to add a third fill variable (exitspeed) to make it look more like a heat map. I have tried adding 'fill=es' to the stat_density_2d line, but the code either ignores the 'fill=es' line or says it cannot find the variable es.

Below are pictures of what my code is plotting now and what I want the plot to look like.

Current Code:

What I Want:

Note: I would still like to have a scale on the right-hand side of the graph.

Does anyone know how to properly add a third variable to a stat_density_2d plot? I am also open to using other plots/packages to construct this heat map. Thanks in advance!

解决方案

There are two problems with your graph:
- First, the different scales (units) as commented. This makes it not possible to simply create a second stat_density for exitspeed as I have suggested in the comment. Also, fill = ..density.. won't work in this case because we are talking about a different variable.
- Second, the coarse x/y values (see below).

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

The problem with the coarse x/y coordinates is, the interpolation is not very smooth. One could change the interpolation parameters, but I don't know how to do this (yet). @JasonAizkalns asked in this question in this direction - but unfortunately there is no answer yet.

More granular x/y coordinates would definitely help though. So why not predict them semi-manually.

What you basically want, is to assign an exit speed value to each x/y coordinate - within your density contour plot ! (Although I personally think it probably doesn't make real sense, because those things are not necessarily related.)

Now - in the following I will predict a value for randomly sampled x/y within (!) the largest polygon of your density contours from your original plot. Let's see:

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

这篇关于将第三个变量添加到stat_density_2d图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:04