簡體   English   中英

向 stat_density_2d 圖添加第三個變量

[英]Adding a 3rd Variable to a stat_density_2d Plot

下面是我在 R 中創建的 stat_density_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, 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()

數據:

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

代碼成功運行,但是,我想添加第三個填充變量(exitspeed),使其看起來更像熱圖。 我曾嘗試將 'fill=es' 添加到 stat_density_2d 行,但代碼要么忽略了 'fill=es' 行,要么說它找不到變量 es。

下面是我的代碼現在正在繪制的圖片以及我想要的圖的外觀。

當前代碼:

在此處輸入圖片說明

我想要的是:

在此處輸入圖片說明

注意:我仍然希望在圖表的右側有一個比例尺。

有誰知道如何將第三個變量正確添加到 stat_density_2d 圖中? 我也願意使用其他圖/包來構建此熱圖。 提前致謝!

您的圖表有兩個問題:
- 首先,評論的不同尺度(單位)。 這使得無法像我在評論中建議的那樣簡單地為 exitspeed 創建第二個stat_density 此外, fill = ..density.. 在這種情況下不起作用,因為我們正在談論不同的變量。
- 其次,粗略的 x/y 值(見下文)。

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

粗略的 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))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM