簡體   English   中英

ggmap + ggplot 不會繪制某些值

[英]ggmap + ggplot will not plot certain values

我正在嘗試獲取一個空間數據集,旋轉它,然后用 ggplot/ggmap 繪制它們。 我已經包含了數據集、用於圍繞原點旋轉數據集的函數以及我希望使用的繪圖方法。

library(ggplot2)
library(ggmap)
library(scales)
source("DEMOFunctions.R")

PlantLAT <- 39.28682
PlantLON <- -96.1172
Emissions <- 12591532084.8523
Resolution <- 0.1

DataPoints <- read.delim("JEC-10000m2.txt", header = TRUE, sep = "")
Origin_DataPoints <- ShiftToOrigin("S", DataPoints, PlantLAT, PlantLON)

Rotated_Origin_DataPoints <- RotateDispersion(Origin_DataPoints, 25)

Rotated_DataPoints <- ShiftToOrigin("U", Rotated_Origin_DataPoints, 
PlantLAT, PlantLON)

Quantiles <- quantile(DataPoints$CO2, c(0.1, 0.955))
qn01 <- rescale(c(Quantiles, range(DataPoints$CO2)))

map <- get_map(location = c(lon = -95, lat = 43), zoom = 6, maptype = 
"terrain", colo = "bw")

ggmap(map) +
  geom_raster(data = DataPoints, aes(x = LON, y = LAT, fill = CO2), 
  interpolate = TRUE) +
  geom_raster(data = Rotated_DataPoints, aes(x = LON, y = LAT, fill = CO2), 
  interpolate = TRUE) +
  scale_fill_gradientn(colours = colorRampPalette(c("limegreen", "yellow", 
  "orange", "red4"))(50),
                   values = c(0, seq(qn01[1], qn01[2], length.out = 2000), 
    1), 
                   limits = c(min(DataPoints$CO2), max(DataPoints$CO2)),
                   name = "Concentration (kg/cbm)",
                   guide = FALSE) +
  coord_cartesian() +
  theme_bw() +
  xlab("Longitude") +
  ylab("Latitude") +
  theme(strip.text.y = element_text(size = 20, colour = "black", face = 
  "bold", angle = -90)) +
  theme(plot.title = element_text(size = 30, face = "bold")) +
  theme(axis.text=element_text(size=15), 
    axis.title=element_text(size=25,face="bold")) +
    theme(axis.title.y = element_text(margin = margin(t = 10, r = 10, b = 
10, l = 10))) +
  theme(plot.margin=unit(c(1,1,1,1),"cm"))

我每次都可以讓“DataPoints”繪圖,但旋轉的“Rotated_DataPoints”有時只繪圖; 這取決於我旋轉多少。 (這可以通過“RotateDispersion”函數中包含的數字進行調整。)

我對這種不一致感到困惑。 (在之前的解決方案嘗試中,我將旋轉分散文件中的小數位數限制為 4 位,但這只是很小的改進,並且仍然存在繪圖不一致。)

“JEC-10000m2.txt”文件可以在這里找到,“DEMOFunctions.R”腳本可以在這里找到。 該腳本包含“ShiftToOrigin”和“RotateDispersion”函數。

在此先感謝您的幫助! 對代碼的格式和稀疏的注釋感到抱歉。 此代碼旨在作為“概念證明”運行。

當您旋轉數據集時,x 或 y 軸上的相鄰點可能會變得如此接近,以至於geom_raster() (或geom_tile()geom_raster()只是一個特例)最終創建寬度/高度為 0 的切片。

讓我們用一個簡單的例子來說明:

library(dplyr)

set.seed(123)
orig <- data.frame(
  x = rep(1:5, each = 4),
  y = rep(1:4, 5),
  z = rpois(20, lambda = 5)
)

orig <- orig %>%
  mutate(t = case_when(x == 1 & y == 1 ~ "C1",
                       x == 1 & y == 4 ~ "C2",
                       x == 5 & y == 4 ~ "C3",
                       x == 5 & y == 1 ~ "C4",
                       TRUE ~ NA_character_))

在旋轉之前,這是圖的樣子(我為 4 個角添加了標簽,以便更容易跟隨旋轉):

p.orig <- ggplot(orig, aes(x = x, y = y, fill = z, label = t)) +
  coord_fixed(xlim = c(0, 6), y = c(0, 5)) +
  theme_bw()

p.orig + geom_point(shape = 22, size = 10) + geom_text() + ggtitle("Unrotated points")

p.orig + geom_raster() + geom_text() + ggtitle("Unrotated raster")

未旋轉

我們可以看到數據點位於垂直於 x / y 軸的直線行和列中。 geom_raster 創建的相應瓦片彼此很好地接觸。

現在讓我們稍微旋轉一下數據RotateDispersion()我改編了RotateDispersion()函數中的相關代碼):

theta = 5/100
rotated <- orig %>%
  mutate(y = x * sinpi(theta) + y * cospi(theta),
         x = x * cospi(theta) - y * sinpi(theta))

p.rot <- ggplot(rotated, aes(x = x, y = y, fill = z, label = t)) +
  coord_fixed(xlim = c(0, 5), y = c(0.5, 5.5)) +
  theme_bw()

p.rot + geom_point(shape = 22, size = 10) + geom_text() + ggtitle("Rotated points")
p.rot + geom_raster() + geom_text() + ggtitle("Rotated raster")

旋轉

geom_points()旋轉沒有任何其他差異(點大小通過size = 10明確控制),但geom_raster()圖中的geom_raster()顯着縮小。

仔細觀察會發現每個瓦片的大小受每個軸上相鄰數據點之間的距離的限制。 (使用 Photoshop 添加的行)

放大

對於某些旋轉角度(例如theta = 25/100 ), geom_tile()將返回一個空白畫布,因為每個圖塊的寬度和高度都被壓縮為 0,而geom_raster()將引發錯誤。

根據您的用例, geom_point()可能比geom_raster()geom_tile()工作得更好。

暫無
暫無

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

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