我想要採用空間數據集,旋轉它,並用ggplot/ggmap繪製它們兩者。我已經包括了數據集,我的函數用於旋轉關於原點的數據集,以及我希望使用的繪圖方法。ggmap + ggplot不會繪製某些值
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」文件可以找到here和「DEMOFunctions.R」腳本可以找到here。該腳本包含「ShiftToOrigin」和「RotateDispersion」功能。
在此先感謝您的幫助!對不起,關於代碼的格式和稀疏的評論。此代碼旨在作爲「概念證明」運行。
太棒了!謝謝! –