library(tidytuesdayR)
library(tidyverse)
library(sf)
library(raster)
library(spData) # spatial data
library(spDataLarge)
library(tmap) # for static and interactive maps
library(leaflet) # for interactive maps
# loading data --------------------------
<- tidytuesdayR::tt_load(2021, week = 19)
tuesdata
# manipulation ---------------
<- tuesdata$water
water $water_source[is.na(water$water_source)] <- "unknown"
water<-water%>%
water_dfarrange(row_id)%>%
clean_names()%>%
mutate(report_date=format(as.Date(report_date,"%m/%d/%Y")),
report_date=as.Date(report_date),
water_source=as.factor(water_source),
status_id=as.factor(status_id))%>%
select(1:6,9)
# loading africa -------------------
= world[world$continent == "Africa", ]
world_africa = st_union(world_africa)
africa
# plotting --------------------
library(extrafont)
library(patchwork)
<-ggplot() +
water_plotgeom_sf(data = africa) +
geom_point(data = water_df,
aes(x=lon_deg,y=lat_deg,color=country_name),
alpha=0.5) +
labs(title="Africa source of water by country",
subtitle="",
caption="Viz. Federica Gazzelloni DataSource: Water Access Points, WPDX TidyTuesday week 19")+
theme_ps()+
theme(legend.position = "none",
plot.margin = margin(5,5,5,5),
plot.title = element_text(size=24,family="xkcd",vjust=-2),
plot.caption = element_text(size=8,family="xkcd",hjust = 0.5,vjust=-2))
# background ---------
library(ggimage)
<-"frame.png"
image
<-ggbackground(water_plot, image, alpha=.7,color="#94BCFF")
final_plot
####### SAVING ######################################
::agg_png(here::here("tidytuesday_Water.png"),
raggres = 320, width = 14, height = 8, units = "in")
final_plot
dev.off()
#### ATTACHING LOGO ############################
library(ggimage)
library(magick)
<-image_read("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/static/plot_logo.png") %>%
tidy_logoimage_resize("300x300")
<- image_read("W19/tidytuesday_Water.png")
final_plot
<- image_composite(final_plot, tidy_logo,
attached_logo operator="atop",
gravity="northeast") # tell R where to put the logo
image_write(attached_logo, path = "tidytuesday_Water.png", format = "png") # save final plot
Week 19 Water————