Mapping different states with geom_sf using facet wrap and scales free - r

First, I am aware of this answer : Mapping different states in R using facet wrap
But I work with object of library sf.
It seems that facet_wrap(scales = "free") is not available for objects plotted with geom_sf in ggplot2. I get this message:
Erreur : Free scales are only supported with coord_cartesian() and
coord_flip()
Is there any option I have missed ?
Anyone has solve the problem without being forced to use cowplot (or any other gridarrange)?
Indeed, here is an example. I would like to show the different French regions separately in facets but with their own x/y limits.
The result without scales = "free"
Scales are calculated with the extent of the entire map.
FRA <- raster::getData(name = "GADM", country = "FRA", level = 1)
FRA_sf <- st_as_sf(FRA)
g <- ggplot(FRA_sf) +
geom_sf() +
facet_wrap(~NAME_1)
The result using cowplot
I need to use a list of ggplots and can then combine them.
This is the targeted output. It is cleaner. But I also want a clean way to add a legend. (I know may have a common legend like in this other SO question :
facet wrap distorts state maps in R)
g <- purrr::map(FRA_sf$NAME_1,
function(x) {
ggplot() +
geom_sf(data = filter(FRA_sf, NAME_1 == x)) +
guides(fill = FALSE) +
ggtitle(x)
})
g2 <- cowplot::plot_grid(plotlist = g)

I know you are looking for a solution using ggplot2, but I found the tmap package could be a choice depends on your need. The syntax of tmap is similar to ggplot2, it can also take sf object. Take your FRA_sf as an example, we can do something like this.
library(tmap)
tm_shape(FRA_sf) +
tm_borders() +
tm_facets(by = "NAME_1")
Or we can use geom_spatial from the ggspatial package, but geom_spatial only takes Spatial* object.
library(ggplot2)
library(ggspatial)
ggplot() +
geom_spatial(FRA) + # FRA is a SpatialPolygonsDataFrame object
facet_wrap(~NAME_1, scales = "free")

Related

Override aesthetics of custom plot

I am not sure exactly how to override aesthetic properties of a custom plot made with ggplot. The only way I could think of right now was using the functionality of the grid package, though is really hackish. Maybe there is a easier way, like using guides or so from ggplot2, though I could't manage to make it work?
Below is an example where I just want to adjust the line width in the graph. Of course, I would like that to trickle down in the legend as well. So, below are my steps with grid, but any simpler solution is greatly appreciated (ideally something that doesn't need grid but just ggplot2, if possible).
library(iNEXT)
library(ggplot2)
library(grid)
# Some custom plot from the iNEXT package
data(spider)
out <- iNEXT(spider, q=0, datatype="abundance")
custom_plot <- ggiNEXT(out)
custom_plot
# Get the grobs
g <- grid.force(ggplotGrob(custom_plot))
# Check the list of names of grobs:
# grid.ls(g)
# View(g$grobs)
# Get an idea about the grob paths
gpaths <- paste(gsub(pattern = "layout::",
replacement = "",
x = grid.ls(g, print = FALSE)$gPath),
grid.ls(g, print = FALSE)$name,
sep = "::")
gpaths[grepl("polyline", gpaths)]
#> [1] "panel.7-5-7-5::grill.gTree.114::panel.grid.minor.y..polyline.107"
#> [2] "panel.7-5-7-5::grill.gTree.114::panel.grid.minor.x..polyline.109"
#> [3] "panel.7-5-7-5::grill.gTree.114::panel.grid.major.y..polyline.111"
#> [4] "panel.7-5-7-5::grill.gTree.114::panel.grid.major.x..polyline.113"
#> [5] "panel.7-5-7-5::GRID.polyline.91"
#> [6] "panel.7-5-7-5::geom_ribbon.gTree.101::geom_ribbon.gTree.95::GRID.polyline.93"
#> [7] "panel.7-5-7-5::geom_ribbon.gTree.101::geom_ribbon.gTree.99::GRID.polyline.97"
# Edit the width of the lines
g <- editGrob(grob = g,
gPath = gpaths[grepl("panel.7-5-7-5::GRID.polyline", gpaths)],
gp = gpar(lwd = c(1,1,1,1)))
plot(g)
Created on 2020-07-22 by the reprex package (v0.3.0)
The answer you are looking for is under "Draw R/E curves by yourself" at
https://cran.r-project.org/web/packages/iNEXT/vignettes/Introduction.html.
Fortunately, the authors of the package have provided the function fortify() along with some code to copy verbatim, to achieve what you desire.
You should copy the following from that section and change the lwd (line width) parameter in the geom_line() function call to your liking.
df <- fortify(out, type=1) # Note the type parameter!
df.point <- df[which(df$method=="observed"),]
df.line <- df[which(df$method!="observed"),]
df.line$method <- factor(df.line$method,
c("interpolated", "extrapolated"),
c("interpolation", "extrapolation"))
ggplot(df, aes(x=x, y=y, colour=site)) +
geom_point(aes(shape=site), size=5, data=df.point) +
geom_line(aes(linetype=method), lwd=1.5, data=df.line) +
geom_ribbon(aes(ymin=y.lwr, ymax=y.upr,
fill=site, colour=NULL), alpha=0.2) +
labs(x="Number of individuals", y="Species diversity") +
theme(legend.position = "bottom",
legend.title=element_blank(),
text=element_text(size=18),
legend.box = "vertical")
I think you're making life overly complicated. Does this approach gives you what you need?
Generate a plot
plot <- mtcars %>% ggplot() + geom_line(aes(x=mpg, y=cyl, colour=as.factor(gear)))
plot
Modify the plot
plot + aes(size=5) + guides(size=FALSE)
The guides call suppresses the legend for size. Obviously, you can delete it if you do want the legend to appear.
Update
Responding to OP's question in the comments. I agree: my suggestion does not modify the ggiNEXT plot as I predicted.
I've done some digging. The diversity curves in the plot are produced by the following statement in the ggiNEXT.iNEXT function
g <- g + geom_line(aes_string(linetype = "lty"), lwd = 1.5) + ...
I find this strange. As far as I know, lwd is not an aesthetic in ggplot2. (And "lty" is not a valid value for the linetype aesthetic. However, lty and lwd are the base R equivalents of ggplot2's linetype and size respectively.)
In case lwd was an undocumented feature, I tried
custom_plot + aes(lwd=3)
But this had no effect.
I then copied the body of the ggiNEXT.iNEXT function into my own function and changed the call to geom_line to read
g <- g + geom_line(aes_string(linetype = "lty"), size = 1.5)
Calling my new function produced a plot identical (to my eye at least) to that produced by the original ggiNEXT.iNEXT call. Then
custom_plot <- myPlot(out)
custom_plot
custom_plot + aes(size=3) + guides(size=FALSE)
Produced the predicted changes. So my best suggestion is either (1) to create a local version of ggiNEXT.iNEXT and load it whenever you need to make this modification [Of course, you then need to make sure you update your local copy in line with any changes to the "official" version] or (2) to create the graph from scratch. Looking at the source code for ggiNEXT.iNEXT, it's not that complicated.
This might be worth raising as an issue with the authors of iNEXT.

Ggplot and sf for overlaying two layers of polygons (.shp)

I have two polygon '.shp' files. I need one to appear on the map by filling in one variable and the other to appear only on borders, overlapping the first.
I have already used 'ggplot2' and 'sf'.
I plotted a map ('map1'), which is layered with polygons, using 'ggplot' and 'geom_sf'.
I use a variable ('var1') contained in 'map1' as a 'fill'.
Now, I need to add (overlay) another layer of polygons on top ('map2'). This will have to be 'transparent fill' or 'no fill'. Only appearing the outline of the borders.
library(ggplot2); library(sf)
map1 <- st_read("m1.shp") #reading polygon layer map 1
map2 <- st_read("m2.shp")#reading polygon layer map 2
g <- ggplot(map1, aes(fill = var1)) +
geom_sf()
How can i add 'map2' for overlay this map?
The idea would be:
g <- ggplot(map1, aes(fill = var1)) +
geom_sf() +
ggplot(map2, aes()) +
geom_sf()
#Error: Don't know how to add ggplot(map2, aes()) to a plot
Every geom_SOMETHING() function has a data argument where you can configure the data you are using. This argument plays the same role as the data argument in the ggplot() function. When you specify data in ggplot, all the other geom_SOMETHING() function inherit the argument. The same happens with the arguments in aes()
So the first recommendation is remove the data = map1 and aes arguments from ggplot and add it to the geom_sf function.
g <- ggplot() + geom_sf(map1, aes(fill = var1)) + geom_sf(map2)

ggplot, ggsave & coord_map/quickmap: how to save large spatial objects and get the projection right?

I have a largish polyline shapefile (Bavarian rivers, which can be accessed here) which I would like to plot and save via ggplot. This can easily be done via e.g. this code:
library(ggplot2)
library(rgdal)
library(sp)
library(rgeos)
riv <- readOGR(paste0(getwd(),"\\rivers_bavaria","rivers_bavaria"))
riv1 <- subset(riv,WDM=="1310"|WDM=="1320")
riv2 <- subset(riv,WDM=="1330")
p <- ggplot() +
geom_line(data=riv1, aes(x=long, y=lat, group=group), color="dodgerblue", size=1) +
geom_line(data=riv2, aes(x=long, y=lat, group=group), color="dodgerblue")
ggsave(paste0(getwd(),"\\riv.tiff",p,device="tiff",units="cm",dpi=300)
This is not exactly efficient, due to the large file size, but it works. However, without further specifying aspect ratio or projection, the dimensions of the output file are defined by the plot window - not desirable for maps. This can be remedied by using coord_quickmap().
p1 <- ggplot() +
geom_line(data=riv1, aes(x=long, y=lat, group=group), color="dodgerblue", size=1) +
geom_line(data=riv2, aes(x=long, y=lat, group=group), color="dodgerblue") +
coord_quickmap()
p1
Unfortunately, the projection is completely off. I have tried coord_map() for a better result, but due to the large file size, it takes forever and is therefore not a realistic option. Simplifying the polyline via gLinemerge() produces a much smaller object, but cannot be handled by ggplot, as it is a SpatialLines object. Using fortify() or data.frame() to coerce it into a ggplot-friendly data frame format also produces Error: ggplot2 doesn't know how to deal with data of class SpatialLines.
I'm therefore desperately looking for a workflow that will allow me to plot and save this kind of spatial data in good quality with ggplot. Any suggestions will be much appreciated!
Here's a quick walkthrough with sf. I recommend the sf vignettes and docs to see more details of any of the functions. I'm first reading the shapefile in as an sf object using sf::st_read, then filtering, mutating, and selecting the same as you would in dplyr to get a smaller version of the shape.
library(tidyverse)
library(sf)
rivers_sf <- st_read("rivers_bavaria/rivers_bavaria.shp") %>%
filter(WDM %in% c("1310", "1320", "1330")) %>%
mutate(name2 = ifelse(WDM == "1330", "river 2", "river 1")) %>%
select(name2, NAM, geometry)
The object is pretty big, and will be very slow to plot, so I simplified it by uniting the geometries by name, then using st_simplify. There's also rmapshaper::ms_simplify, which uses Mapshaper and which I prefer for better control over how much information you keep. Then to show a CRS transformation, I picked a projection from Spatial Reference for Germany.
riv_simple <- rivers_sf %>%
group_by(name2, NAM) %>%
summarise(geometry = st_union(geometry)) %>%
ungroup() %>%
st_simplify(preserveTopology = T, dTolerance = 1e6) %>%
st_transform(31493)
The dev version of ggplot2 on GitHub has a function geom_sf for plotting different types of sf objects. To get this version, run devtools::install_github("tidyverse/ggplot2").
geom_sf has some quirks, and works a little differently from other geoms, but it's pretty versatile. I believe it's being included in the next CRAN release. geom_sf has corresponding stat_sf and coord_sf. By default, it plots graticule lines; to turn those off, add coord_sf(ndiscr = F).
ggplot(riv_simple) +
geom_sf(aes(size = name2), color = "dodgerblue", show.legend = "line") +
scale_size_manual(values = c("river 1" = 1, "river 2" = 0.5)) +
theme_minimal() +
coord_sf(ndiscr = F)
Hope that helps you get started!

Is it possible to desaturate a ggplot easily?

Is it possible to desaturate a ggplot easily?
In principle, there could be two possible strategies.
First, apply some function to a ggplot object (or, possibly, Grob object) to desaturate all colors. Second, some trick to print ggplot desaturated while rendering a .rmd file. Both strategies would be ok for me, but first one is, of course, more promissing.
Creating ggplot in greys from the beginning is not a good option as the idea is to have the same plot as if it was printed in shades of grey.
There were some similar questions and remarkably good answers on how to perform desaturation in R. Here is a convenient way to desaturate color palette. And here is the way of desaturating a raster image. What I'm looking for, is a simple way of desaturating the whole ggplot.
Just came across this question. The experimental package colorblindr (written by Claire McWhite and myself) contains a function that can do this in a generic way. I'm using the example figure from #hrbrmstr:
library(ggplot2)
library(viridis)
gg <- ggplot(mtcars) +
geom_point(aes(x=mpg, y=wt, fill=factor(cyl), size=factor(carb)),
color="black", shape=21) +
scale_fill_viridis(discrete = TRUE) +
scale_size_manual(values = c(3, 6, 9, 12, 15, 18)) +
facet_wrap(~am)
gg
Now let's desaturate this plot, using the edit_colors() function from colorblindr:
library(colorblindr) # devtools::install_github("clauswilke/colorblindr")
library(colorspace) # install.packages("colorspace", repos = "http://R-Forge.R-project.org") --- colorblindr requires the development version
# need also install cowplot; current version on CRAN is fine.
gg_des <- edit_colors(gg, desaturate)
cowplot::ggdraw(gg_des)
The function edit_colors() takes a ggplot2 object or grob and applies a color transformation function (here desaturate) to all colors in the grob.
We can provide additional arguments to the transformation function, e.g. to do partial desaturation:
gg_des <- edit_colors(gg, desaturate, amount = 0.7)
cowplot::ggdraw(gg_des)
We can also do other transformations, e.g. color-blind simulations:
gg_des <- edit_colors(gg, deutan)
cowplot::ggdraw(gg_des)
Finally, we can manipulate line colors and fill colors separately. E.g., we could make all filled areas blue. (Not sure this is useful, but whatever.)
gg_des <- edit_colors(gg, fillfun = function(x) "lightblue")
cowplot::ggdraw(gg_des)
As per my comment above, this might be the quickest/dirtiest way to achieve the desaturation for a ggplot2 object:
library(ggplot2)
set.seed(1)
p <- qplot(rnorm(50), rnorm(50), col="Class")
print(p)
pdf(file="p.pdf", colormodel="grey")
print(p)
dev.off()
I tried this with the new viridis color palette since it desaturates well (i.e. it should be noticeable between the colored & non-colored plots):
library(ggplot2)
library(grid)
library(colorspace)
library(viridis) # devtools::install_github("sjmgarnier/viridis") for scale_fill_viridis
gg <- ggplot(mtcars) +
geom_point(aes(x=mpg, y=wt, fill=factor(cyl), size=factor(carb)),
color="black", shape=21) +
scale_fill_viridis(discrete = TRUE) +
scale_size_manual(values = c(3, 6, 9, 12, 15, 18)) +
facet_wrap(~am)
gb <- ggplot_build(gg)
gb$data[[1]]$colour <- desaturate(gb$data[[1]]$colour)
gb$data[[1]]$fill <- desaturate(gb$data[[1]]$fill)
gt <- ggplot_gtable(gb)
grid.newpage()
grid.draw(gt)
You end up having to manipulate on the grob level.
Here's the plot pre-desaturate:
and here's the plot post-desature:
I'm trying to figure out why the legend got skipped and this may miss other highly customized ggplot aesthetics & components, so even while it's not a complete answer, perhaps it might be useful (and perhaps someone else can tack on to it or expand on it in another answer). It should just be a matter of replacing the right bits in either the gb object or gt object.
UPDATE I managed to find the right grob element for the legend:
gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[4]]$gp$fill <-
desaturate(gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[4]]$gp$fill)
gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[6]]$gp$fill <-
desaturate(gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[6]]$gp$fill)
gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[8]]$gp$fill <-
desaturate(gt$grobs[[12]][[1]][["99_9c27fc5147adbe9a3bdf887b25d29587"]]$grobs[[8]]$gp$fill)
grid.newpage()
grid.draw(gt)
The machinations to find the other gp elements that need desaturation aren't too bad either.

ggplot2; Adding points from a different dataset and a raster/matrix data to a plot of a spatialpolygonsdataframe

I tried this but i get Error in eval(expr, envir, enclos) : object 'group' not found. pj4s is an object containing the projection in latlong.
my code
fx.ggplot<-function(ctry,aesfill="id",scalefill="Country",pathcol="white"){
#ctry is a shapefile of countries
ctry#data$id = rownames(ctry#data)
ctry.points = fortify(ctry, region="id")
ctry.df = join(ctry.points, ctry#data, by="id")
(txt<-paste("p<-ggplot(ctry.df) + aes(long,lat,group=group,fill=",aesfill,")"))
eval(parse(text=txt))
p<-p+ geom_polygon() +
geom_path(color=pathcol) +
coord_equal() +
scale_fill_brewer(scalefill)
return(p)
}
#generate a grid of points
xo<-seq(25,45,0.5)
yo<-seq(-15,5,0.5)
head(xy<-cbind(expand.grid(xo,yo)));names(xy)<-c("lon","lat")
head(xy.sp <- SpatialPoints(xy,proj4string=pj4s))
Overlay<-over(xy.sp,ctry)
xy<-xy[!apply(Overlay, 1, function(x) any(is.na(x))),]
#plot
(p<-fx.ggplot(ctry))
(P<-p+geom_point(data=xy, aes(x=lon, y=lat))) #addpoints returns Error
ggplot()+geom_point(data=xy, aes(x=lon, y=lat)) #This plots the points (as below) without error but in a new plot.
#matrix
mat.ctry<-fx.polygon2raster2array(shp=ctry,xo,yo,cTim=1)
mat<-cbind(expand.grid(xo,yo),c(mat.ctry));names(mat)<-c(names(xy),"Z")
mat<-mat[!apply(mat, 1, function(x) any(is.na(x))),]
p+geom_raster(data=mat,aes(x=lon,y=lat,colour="red"))
#returns Error
yet ggplot()+geom_raster(data=mat,aes(x=lon,y=lat,colour="red")) returns what is expected.
So where am i failing?
In case anyone else can't get the accepted answer work for them: a key is what #aosmith noted in a comment to the original question.
Adding the geom_point(data=xy) creates a new layer on the plot. ggplot2 expects this new layer to match all the aesthetics of the original layer, so you have to specifically define any differences. In this case, the original geom_polygon layer uses group and fill aesthetics but the geom_point layer does not. Therefore, you must specify group=NULL and fill=NULL for geom_point, like so:
(myplot + geom_point(data=xy, aes(group=NULL, fill=NULL)))
Let's break down what is going on and fix some things. First, load the necessary libraries for the code/example.
library("rgeos")
library("sp")
library("ggplot2")
library("plyr")
Looking at your example map, I figured out which east African countries you had. I pulled shape files for them from the rworldmap package. These may not be as nice as your shape files, but they will do for now.
library("rworldmap")
data(countriesLow)
ctry <- countriesLow[countriesLow$ISO3.1 %in% c("TZA", "KEN", "UGA", "RWA", "BDI"),]
Create the grid of points you had. I tidied up the code some, dropping some calls to head and simplifying the subsetting code.
#generate a grid of points
xy<-expand.grid(long=seq(25,45,0.5),lat=seq(-15,5,0.5))
xy.sp <- SpatialPoints(xy, proj4string=CRS(proj4string(ctry)))
Overlay<-over(xy.sp,ctry)
xy<-xy[!is.na(Overlay$ISO3.1),]
Now your fx.ggplot call can be re-written as
fx.ggplot<-function(ctry, aesfill="id", scalefill="Country", pathcol="white") {
##ctry is a shapefile of countries
ctry#data$id = rownames(ctry#data)
ctry.points = fortify(ctry, region="id")
ctry.df = join(ctry.points, ctry#data, by="id")
ggplot(ctry.df, aes(long, lat)) +
geom_polygon(aes_string(group="group", fill=aesfill)) +
geom_path(colour = pathcol) +
scale_fill_brewer(scalefill)
}
The overall aes is just long and lat; geom_polygon needs the additional aesthetics group and fill, and those are set using aes_string since the name of the variable for fill is being passed into the function.
(p <- fx.ggplot(ctry))
There are some artifacts in this version but that is (likely) do the shape file I was using; I didn't see them in the plot you showed, so you should not have a problem with that.
p + geom_point(data=xy)

Resources