gplot
is great! Thanks for creating. I'm having one weird issue when aggregating a raster to a lower resolution prior to plotting it with gplot
(rather than relying on the maxpixels
arguement). This seems to be something deeper within ggplot
, but it's related to the approach used by maxpixels
, so there might be some way gplot
can fix it.
I've been using gplot
very successfully to map my data. Here is an example plot, where the supplied raster to gplot
has a resolution of ~1e10. That's way too many pixels for my eye to see - and also crashes R if I try to plot it anyway.
midwest.rast <- raster("Midwest_PI_Raster.tif")
midwest.rast
class : RasterLayer
dimensions : 75891, 127386, 9667450926 (nrow, ncol, ncell)
resolution : 10, 10 (x, y)
extent : 18855, 1292715, 1453065, 2211975 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
data source : /Users/Midwest_PI_Raster.tif
names : Midwest_PI_Raster
values : 2, 18 (min, max)
counties <- readOGR("cb_2016_us_county_500k/cb_2016_us_county_500k.shp")
midwest_counties <- counties[counties$STATEFP %in% c(17, 39, 18, 29),]
gplot(midwest.rast, maxpixels = 1e+7) +
geom_tile(aes(fill = value)) +
scale_fill_viridis(option = "magma", na.value = "transparent") +
theme_void() +
labs(fill = NULL) +
coord_equal() +
guides(fill = guide_colourbar(title.hjust = 0.5, nbin = 100, label.position = "left")) +
theme(legend.position = c(0.8, 0.25)) +
geom_polygon(data = midwest_counties,
aes(x = long, y = lat, group = group),
color = "black",
fill = "transparent",
size = 0.5)
![midwest_pi_raster-7](https://user-images.githubusercontent.com/11711566/28240652-2c209242-694b-11e7-8b1b-5636d79b2bdf.jpg)
However, I realized that I could speed up my prior calculations by using aggregate
to reduce the resolution of my raster first. I've spared the calculations here, but just simply aggregate
the original raster using fact=31
to reduce the resolution to ~1e7. This is the same resolution that I was having gplot
go to anyway.
midwest.rast.agg <- aggregate(midwest.rast, fact = 31)
midwest.rast.agg
class : RasterLayer
dimensions : 2449, 4110, 10065390 (nrow, ncol, ncell)
resolution : 310, 310 (x, y)
extent : 18855, 1292955, 1452785, 2211975 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
data source : /private/var/folders/4p/mpc8rykn1svfp0dd6639lkw40000gn/T/RtmpNE6q5T/raster/r_tmp_2017-07-15_100058_626_42210.grd
names : Midwest_PI_Raster
values : 2, 18 (min, max)
gplot(midwest.rast.agg, maxpixels = 1e+7) +
geom_tile(aes(fill = value)) +
scale_fill_viridis(option = "magma", na.value = "transparent") +
theme_void() +
labs(fill = NULL) +
coord_equal() +
guides(fill = guide_colourbar(title.hjust = 0.5, nbin = 100, label.position = "left")) +
theme(legend.position = c(0.8, 0.25)) +
geom_polygon(data = midwest_counties,
aes(x = long, y = lat, group = group),
color = "black",
fill = "transparent",
size = 0.5)
The resulting maps looks identical EXCEPT that the alpha
value for the fill is clearly less than 1. This seems totally random to me. I've even tried to explicitly set the alpha
in scale_fill_viridis
to 1, but that's the default value anyway and doesn't do anything.
![midwest_pi_raster-7-new-agg](https://user-images.githubusercontent.com/11711566/28240721-22c4a390-694c-11e7-99b7-73a8be5f4376.jpg)
Furthermore, if you zoom in, you can see what appears to be a phantom grid from ggplot. Not sure why that's there either, since I used theme_void()
.
![screen shot 2017-07-15 at 10 56 30 am](https://user-images.githubusercontent.com/11711566/28240732-4cde49ec-694c-11e7-9097-7223b191fe33.JPG)
To be fair, even if I manually convert this aggregated raster to a SPDF and then supply it directly to ggplot
and geom_tile
(no gplot
), the same issue happens. So I guess it because a question of (1) what is gplot
doing with maxpixels
that makes the saturation correct and (2) is there an update to gplot
that could prevent this weird desaturation when supply an aggregated raster?