library(tidyverse)
library(geoR)
library(sf)
library(afrilearndata)
library(cowplot)
# set the fonts
library(showtext)
library(sysfonts)
library(extrafont)
::showtext_auto()
showtext::showtext_opts(dpi=320)
showtextfont_add_google(name = "Mukta",family="Mukta")
Overview
Malaria prevalence in children
, data recorded at villages in The Gambia, Africa
. Data is from {geoR}
package.
Load the libraries
Load data and transform UTM coords into Lat and Long
data(package="geoR")
# ?gambia
%>% head gambia
<- gambia %>%
gambia1 select(x,y) %>%
::st_as_sf(coords = c(1,2),
sfcrs = "+proj=utm +zone=28") %>%
# utm tranformation to longlat
::st_transform(crs = "+proj=longlat +datum=WGS84") %>%
sf::st_coordinates() %>%
sfcbind(gambia) %>%
rename(long=X,lat=Y)
# gambia.borders
# gambia.borders %>%DataExplorer::profile_missing()
# NA in the borders coords
st_write(gambia.borders,
"gambia_borders.csv",
layer_options = "GEOMETRY=AS_XY")
# replacing null geometries with empty geometries
= st_read("gambia_borders.csv",
gambia.borders2 options = c("X_POSSIBLE_NAMES=X",
"Y_POSSIBLE_NAMES=Y"),
crs="+proj=utm +zone=28")
<- gambia.borders2 %>%
gambia.borders3 ::st_as_sf(coords = c(1,2),
sfcrs = "+proj=utm +zone=28") %>%
# utm tranformation to longlat
::st_transform(crs = "+proj=longlat +datum=WGS84")
sf
<- gambia.borders3 %>%
gambia.borders4 ::st_coordinates() %>%
sfas.data.frame()%>%
rename(long=X,lat=Y)
# check the bbox
# bbox = left,bottom,right,top
# bbox = min Longitude , min Latitude , max Longitude , max Latitude
# gambia.borders3%>%
# st_bbox()
Make the map
<- gambia1 %>%
map filter(pos==1) %>%
ggplot(aes(long,lat)) +
geom_point(shape=21,stroke=0.5,size=2,color="grey40") +
geom_point(aes(color=age)) +
geom_point(data=gambia1 %>%
filter(pos==1,phc==1) ,
inherit.aes = TRUE,
shape="H",stroke=0.1,size=2,color="red") +
coord_map()+
scale_color_gradient2() +
geom_path(data = gambia.borders4, aes(long,lat),size=0.1)+
labs(title="Malaria prevalence in children",
subtitle="data recorded at villages in The Gambia, Africa",
caption="#30DayMapChallenge 2022 day 1 Points\nDataSource: Gambia data from {geoR} package\nMap: Federica Gazzelloni",
color="Age in days")+
::theme_map()+
ggthemestheme(text=element_text(family="Mukta"),
legend.position = c(0.1,-0.1),
legend.direction = "horizontal",
legend.background = element_blank(),
legend.key.size = unit(10,units="pt"),
legend.text = element_text(size=4),
legend.title = element_text(size=5),
plot.title = element_text(vjust=3,hjust=0.5),
plot.subtitle = element_text(vjust=3,hjust=0.5),
plot.caption = element_text(vjust=-9,hjust=0.5),
plot.title.position = "panel",
# margin(t = 0, r = 0, b = 0, l = 0, unit = "pt")
plot.margin = margin(10,1,20,1,unit = "pt"),
plot.background = element_rect(color = "black",fill="grey95",size=0.05),
panel.background = element_rect(color = "grey95",fill="grey95"))
# add a little Africa on a side
# remotes::install_github("afrimapr/afrilearndata")
# library(afrilearndata)
<- afrilearndata::africountries
afr_sf # afr_sf%>%head
<- afr_sf%>%filter(name_sw=="Gambia") gamb_sf
<- ggplot()+
afrimapgeom_sf(data=afr_sf,
aes(fill=pop_est),
size=0.1,
show.legend = F,alpha=0.1) +
geom_sf(data=gamb_sf,
fill="brown",alpha=0.9)+
theme_void()
Draw the plot
::ggdraw()+
cowplotdraw_plot(map)+
draw_plot(afrimap,scale = 0.2,
x=0.3,y=0.25)
Save it
ggsave("day1_points.png",
dpi=300,
bg="grey95",
width = 6,height = 3.5)