library(tidyverse)
Overview
This is day 8 - blue
for #30DayMapChallenge. I am looking at the ggplot2-book map chapter.
In this case I’d like to understand how to make modification of a .tif image positioning it inside a ggplot.
Load the {ozmaps} for the geometry of Australia.
library(ozmaps)
library(sf)
<- ozmaps::ozmap_states
oz_states
oz_states
st_crs(oz_states) = st_crs(4283)
%>%
oz_statesggplot()+
geom_sf()+
coord_sf()
::tm_shape(oz_states) +
tmap::tm_polygons() tmap
First sat vis of Australia
Then, select the raster images from source.
See which raster images are available with loading the {bomrang} package and using the syntax below.
Once you have found the file name, you can obtain the file searching on google tab with this string: “ftp://ftp.bom.gov.au/anon/gen/gms/” with the final name of the file
For example google this, it might take a little while: ftp://ftp.bom.gov.au/anon/gen/gms/IDE00422.202001072100.tif
It pops up a window asking you to identify as a “guest”, (it might probably saying that it couldn’t connect you, but then split all the pictures. You need to search for the desired picture, if it didn’t prompt with the one you were searching for, mind it is .tif type of file and you might need to choose a picture from your current year, such as 2021 - as the pictures from 2020 are not available anymore.)
# sf::gdal_read()
# bomrang::get_available_imagery()
# with - bomrang - find the avalable images
<- bomrang::get_available_imagery() %>%
files ::str_subset("202001072100")
stringr
# use curl_download() to obtain a single file, and purrr to
# vectorise this operation
::walk2(
purrr.x = paste0("ftp://ftp.bom.gov.au/anon/gen/gms/", files),
.y = file.path("raster", files),
.f = ~ download.file(url = .x, destfile = .y)
)
Set the path of the image that you have downloaded and saved inside a “raster” folder in you project subfolder.
<- file.path("/Users/federica/Documents/R/R_general_resources/30DayMapChallenge/day8_blue/raster", "IDE00422.202111072100.tiff")
img_vis <- file.path("/Users/federica/Documents/R/R_general_resources/30DayMapChallenge/day8_blue/raster", "IDE00421.202111072100.tiff") img_inf
Read the picture with {stars}, it releases information about the picture but we miss the coordinates??
library(stars)
<- read_stars(img_vis, RasterIO = list(nBufXSize = 600, nBufYSize = 600))
sat_vis <- read_stars(img_inf, RasterIO = list(nBufXSize = 600, nBufYSize = 600)) sat_inf
A ggplot can be made with the image using a geom_stars()
<- ggplot() +
blue_world geom_stars(data = sat_vis) +
coord_equal()
blue_world
The image can be easily modified:
<- blue_world +
final labs(title="Globe Observer",
subtitle="Sat View of Australia\n#30DayMapChalleng Day 8 - Blue",
caption="Datasource:Australian Bureau of Meterorology (BOM)\nInfographic: Federica Gazzelloni")+
::theme_map()+
ggthemestheme(legend.position = "none",
plot.background = element_rect(color="#1a1c3d",fill="#1a1c3d"),
plot.title = element_text(color="#9a9ddb",size=35),
plot.subtitle = element_text(color="#9a9ddb",size=15),
plot.caption = element_text(color="#9a9ddb",size=12))
# save final plot
::agg_png("~/Documents/R/R_general_resources/30DayMapChallenge/day8_blue/blue.png",
raggres = 320, width = 11, height = 8, units = "in")
finaldev.off()
Second sat vis of Australia
library(showtext)
library(extrafont)
fonts()
loadfonts()
font_add_google("Gochi Hand", "gochi")
font_add_google("Schoolbell", "bell")
font_add_google('Syncopate', 'Syncopate')
showtext_opts(dpi = 320)
showtext_auto(enable = T)
Choose the image and download it with curl_download()
<- bomrang::get_available_imagery() %>%
files ::str_subset("202001072100")
stringr::curl_download("raster/ftp://ftp.bom.gov.au/anon/gen/gms/IDE00422.202111082100.tif",
curl"sat_vis.tif")
<- file.path("/Users/federica/Documents/R/R_general_resources/Maps/raster", "IDE00422.202111082100.tif")
img_vis library(stars)
<- read_stars(img_vis, RasterIO = list(nBufXSize = 600, nBufYSize = 600))
sat_vis #sat_inf <- read_stars(img_inf, RasterIO = list(nBufXSize = 600, nBufYSize = 600))
<- st_transform(oz_states, crs = st_crs(sat_vis))
oz_states <- oz_capitals %>%
cities st_as_sf(coords = c("lon", "lat"), crs = 4326, remove = FALSE)
<- st_transform(cities, st_crs(sat_vis)) cities
<- ggplot() +
final2 geom_stars(data = sat_vis, show.legend = FALSE) +
geom_sf(data = oz_states, fill = "#6354c4", color = "#a7c2cc",size=0.3) +
geom_sf(data = cities, color = "#15191f") +
coord_sf() +
theme_void() +
#scale_fill_gradient(low = "black", high = "white")+
geom_sf_text(data = cities, mapping = aes(label = city),color="#ced9eb",check_overlap = T,family="gochi") +
labs(title="Globe Observer",
subtitle="Sat View of Australia\n#30DayMapChalleng Day 8 - Blue",
caption="Datasource:Australian Bureau of Meterorology (BOM)\nInfographic: Federica Gazzelloni")+
::theme_map()+
ggthemestheme(text=element_text(family="gochi"),
legend.position = "none",
plot.background = element_rect(color="#1a1c3d",fill="#1a1c3d"),
plot.title = element_text(color="#9a9ddb",size=35,family="Syncopate",hjust=0.5),
plot.title.position = "panel",
plot.subtitle = element_text(color="#9a9ddb",size=15,vjust=-1,hjust=0.07),
plot.caption = element_text(color="#9a9ddb",size=12,hjust=0.8))
# save final plot
::agg_png("~/Documents/R/R_general_resources/30DayMapChallenge/day8_blue/blue2.png",
raggres = 320, width = 11, height = 8, units = "in")
final2dev.off()