2017-10-17 123 views
2

我想要採用空間數據集,旋轉它,並用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」功能。

在此先感謝您的幫助!對不起,關於代碼的格式和稀疏的評論。此代碼旨在作爲「概念證明」運行。

回答

1

當您旋轉數據集時,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") 

unrotated

我們可以看到,數據點位於直排&列,垂直於TH e x/y軸。由geom_raster創建的相應的貼圖很好地相互接觸。

現在讓我們稍微旋轉數據框(我改編自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") 

rotated

geom_points()情節旋轉,沒有任何其他的差異(點大小與顯式控制size = 10),但geom_raster()圖中的圖塊顯着縮小。

仔細一看,發現每個瓦片的大小都受到每個軸上相鄰數據點之間距離的限制。 (線使用Photoshop加入)

zoomed

對於旋轉的某些角度(例如theta = 25/100),geom_tile()將返回一個空白畫布由於寬度&每個瓦片的高度受到壓縮爲0,而geom_raster()將拋出錯誤。

根據您的使用情況,geom_point()可能會比geom_raster()geom_tile()更好地工作。

+0

太棒了!謝謝! –