简体   繁体   中英

ggmap + ggplot will not plot certain values

I am trying to take a spatial dataset, rotate it, and plot both of them with ggplot/ggmap. I have included the dataset, my functions used to rotate the dataset about an origin, and the method of plotting that I wish to use.

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

I can get "DataPoints" to plot every time but the rotated "Rotated_DataPoints" only plots sometimes; it depends on how much I rotate it. (This can be adjusted by the number included in the "RotateDispersion" function.)

I'm confused by this inconsistency. (In a previous attempt at a solution, I restricted the number of decimal places to 4 in the rotated dispersion file but that was only a minor improvement and there were still plotting inconsistencies.)

The "JEC-10000m2.txt" file can be found here and the "DEMOFunctions.R" script can be found here . This script contains the "ShiftToOrigin" and "RotateDispersion" functions.

Thanks in advance for any help! Sorry about the formatting of the code and sparse commenting. This code was intended as a "proof of concept" run.

When you rotate the dataset, adjacent points on the x or y-axis may become so close together that geom_raster() (or geom_tile() , geom_raster() is just a special case) end up creating tiles with 0 width / height.

Let's illustrate with a simple example:

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

Before rotation, this is what the plot looks like (I added labels for the 4 corners so that it's easier to follow the rotation):

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

未旋转

We can see that the data points are located in straight rows & columns, perpendicular to the x / y axes. The corresponding tiles created by geom_raster touch one another nicely.

Now let's rotate the dataframe slightly (I adapted the relevant code from the RotateDispersion() function):

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

旋转

The plot for geom_points() rotated without any other difference (point size is controlled explicitly with size = 10 ), but the tiles in the geom_raster() plot shrunk significantly.

A closer look reveals that the size of each tile is limited by the distance between adjacent data points on each axis. (lines added using Photoshop)

放大

For some angles of rotation (eg theta = 25/100 ), geom_tile() will return a blank canvas since the width & height of each tile get squeezed to 0, while geom_raster() will throw an error.

Depending on your use case, geom_point() may work better than geom_raster() or geom_tile() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM