R - ggplot with facets: placing facets on the map - r

I visualize some data using ggplot2 package with facets. Data can be, e.g., distribution of some values over different continents (or cities, countries etc). Here are some toy data and a standard solution for making a primary plot with facet_grid() function.
library(ggplot2) # key library
library(reshape2) # to convert to long format
databas<-read.csv(data=
"continent,apples,bananas
North America,30,20
South America,15,34.5
Europe,15,19
Africa,5,35")
databaslong<-melt(databas) # default conversion, will make first col.as id
# plotting as colored bars
ggplot(databaslong,aes(x=variable,y=value,fill=variable))+geom_col()+facet_grid(.~continent)
Following is predictably produced:
But now I would like to have more control on the positions of facets. Particularly, it seems natural to put each on the world map onto respective positions on continents, that is some action typical for tmap or similar packages. For example, use native ggplot instrumentality:
mapWorld <- borders("world", colour="gray50", fill="gray50")
ggplot() + mapWorld
and to get like such (manually combined these two layers in inkscape):
Is it possible to achieve such a combination of ggplot and mapping packages in a programmable way?

Related

How do I plot one of my categorical variables as either differennt colours or shapes using ggplot?

I'm a beginner to R programming and mostly use it for basic data analysis for political science and economics and as such am familiar with only a very selective set of functions. That being said I was playing around with data visualization of India's COVID19 district level data from here and wanted to visualize "States" variable in different colors or shapes but it isn't happening.
Please help me out with this.
ggplot(district_wise,aes(x=Confirmed,y=Deceased)+ geom_point()
ggplot(district_wise,aes(x=Confirmed,y=Deceased),shape=States)+ geom_point()
ggplot(district_wise,aes(x=Confirmed,y=Deceased),col=States)+geom_point()
The Plot
Here are two plots one for points shapes by categorical variable State_Code and the other for colors by State_Code.
In the case of point shapes I first create a vector of shape numbers since ggplot only accepts 6 different shapes. The way to increase this number is with scale_shape_manual. And the values between 26 and 31 must be removed from the shapes vector, see help("points").
library(ggplot2)
pch <- c(1:25, 33:127)
pt_shapes <- pch[1:length(unique(district_wise$State_Code))]
ggplot(district_wise,aes(x=Confirmed,y=Deceased,shape=State_Code)) +
geom_point() +
scale_shape_manual(values=pt_shapes)
ggplot(district_wise,aes(x=Confirmed,y=Deceased,color=State_Code)) +
geom_point()

Heat Map in ggplot(), getting all the layers plotted

I am making a heat map of flooding incidents in the UK. I am following the example listed here. However, I am using a different base map from the example and it won't show up on the map. For my base map I use shapefiles provided by the UK gov, found here, and named it uk.shp, an sf object. Flooding data is proprietary and I cannot share but the original format is polygon shapefile. I then turn those shapefiles into gridded points so I can plot a continuous heat map, this sf object is named pt.shp.
Here is the the base map, original shapefile, and gridded points overlaid for context. You can see there are many floods here, often laying under the same point. I constructed a grid sf object that repeats a given point and uses flood ID as the unique identifier. Below is an example of the data with proprietary information removed. One possible issue I can think of is st_intersection returned the lat and long of the flooding shapefile (which I set as the mapping aes()) but the point's mapping coords are listed in the geometry column of the data.
However, when I use stat_density2d() with my base map, the continuous plot disappears. Below is my plotting code w/ each mapping iteration.
# base map plots
base <- ggplot()+
geom_sf(data=uk.shp)
# Plot density of the points
ggplot()+
stat_density2d(data=pt.shp, aes(x=long, y=lat, fill = ..density..), geom='tile', contour = F)
# base map shows up w/o density map? But legend exists so it is being mapped...
base +
stat_density2d(data=pt.shp, aes(x=long, y=lat, fill = ..density..), geom='tile', contour = F, alpha = .5) +
viridis::scale_fill_viridis(option='inferno')
Some issues are clear, like the plotting window of the heatmap has different dimensions than the base map. However, my main issues is I can't overlay the two plots and I don't know why.

R - ggplot shapefile small features overlaped by bigger ones

when I use ggplot2::ggplot() to create a map using a shapefile I have the problem that small features overlaped by bigger ones. Please note image of the Problem: ggplot overlays the small county by the bigger one.
Please use this shapefile as input data.
load("~/Germany_Bremen_LowerSax_NUTS1.Rdata") # Please use input data mentioned above
library(ggplot2)
plot(shp.nuts.test) # normal plot with visible borders.
shp.f <- fortify(shp.nuts.test)
Map <- ggplot(shp.f, aes(long, lat, group = group, fill = id))+
geom_polygon()
Map
Is there any possibility to change the plot order of the shapefile within ggplot?
Any help appreciated! Thanks!
There are a couple of options:
Reorder factors so that the lower levels plot on top of the higher ones.
Add another layer of the hidden group over the plot (shown below)
library(dplyr)
ggplot(shp.f, aes(long, lat, group = group, fill = id))+
geom_polygon()+
geom_polygon(aes(long,lat), data=filter(shp.f, group=='4.1'))
I personally prefer option 2, because it is a huge pain reordering factors and can easily result in unintended consequences. In addition, you could handle more layers on top. Note that the filter function requires the dplyr library (more on dplyr use).

geom_line only connects points on horizontal lines instead all points

I've written something in R using ggplot2 and don't know why it behaves as it does.
If I plot my data using geom_point and geom_line it is supposed to draw lines trough those points. but instead of connecting all the points it only connects those that are on a horizontal line. I don't know how to handle this.
This is a simple version of the code:
date<-c("2014-07-01","2014-07-02","2014-07-03",
"2014-07-04","2014-07-05","2014-07-06",
"2014-07-07")
mbR<- c(160,163,169,169,169,169,169)
mbL<- c(166,166,166,166,NA, NA, NA)
mb<-data.frame(mbR,mbL)
mb<-data.frame(t(as.Date(date)),mb)
colnames(mb)<-c("Datum","R","L")
mb$Datum<-date
plot1<-ggplot(mb,aes(x=mb$Datum,y=mb$R))+
geom_point(data=mb,aes(x=mb$Datum,y=mb$R,color="R",size=2),
group=mb$R,position="dodge")+
geom_line(data=mb,aes(y=mb$R,color="R",group=mb$R))+
geom_point(aes(y=mb$L,color="L",size=2),position="dodge")
plot1
I used group, otherwise I wouldn't have been able to draw any lines, still it doesn't do what I intended.
I hope you guys can help me out a little. :) It may be a minor fault.
First, melt your data to long format and then plot it. The column called variable in the melted data is the category (R or L). The column called value stores the data values for each instance of R and L. We group and color the data by variable in the call to ggplot, which gives us separate lines/points for R and L.
Also, you only need to provide the data frame and column mappings in the initial call to ggplot. They will carry through to geom_point and geom_line. Furthermore, when you provide the column names, you don't need to (and shouldn't) include the name of the data frame, because you've already specified the data frame in the data argument to ggplot.
library(reshape2)
mb.l = melt(mb, id.var="Datum")
ggplot(data=mb.l, aes(x=Datum, y=value, group=variable, color=variable)) +
geom_point(size=2) +
geom_line()

How to use regions' names as labels in spatial Data?

I use the following code to visualize some SpatialPolygonsDataFrame with ggplot2:
require(shapefiles)
require(sp)
xx <- readShapeSpatial(system.file("shapes/sids.shp", package="maptools")[1],
IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
xx.ff <- fortify(xx,region="NAME")
x <- ggplot(xx.ff) +
aes(long,lat, fill = id) +
geom_polygon()
to end up with:
Assume that I am using fill to represent something else, like regional theft rate. Thus I want to label those regions with their names. is there a way to write the regions name right onto the map into the corresponding region, e.g. I want to write Halifax right onto the region.
Have a look at similar/related questions I asked:
ggplot centered names on a map
Improve centering county names ggplot & maps
Plot county names on faceted state map (ggplot2)

Resources