I'm trying to fix my caption but I'm having trouble creating it. I wanted a caption for the classes (fill), the shape limit (color), the points (color) and the grid (fill=NA). I put them all in aes () but I don't have the expected result. Can anybody help me? Thanks!
library(geobr)
library(sf)
library(ggplot2)
library(ggspatial)
#Directory
getwd()
#Download spatial data ------------------------------------------------
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.dbf",
destfile = "SP_3543907_USO.dbf", mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.prj",
destfile = "SP_3543907_USO.prj", mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shp",
destfile = "SP_3543907_USO.shp", mode = "wb")
download.file(url = "http://geo.fbds.org.br/SP/RIO_CLARO/USO/SP_3543907_USO.shx",
destfile = "SP_3543907_USO.shx", mode = "wb")
#Import spatial data --------------------------------------------------
uso <- sf::st_read("SP_3543907_USO.shp")
uso
plot(uso$geometry)
#Area limit
rio_claro_limit <- geobr::read_municipality(code_muni = 3543907, year = 2015)
rio_claro_limit
plot(rio_claro_limit$geom)
#Random sample points
set.seed(123)
pts <- st_sample(uso, size = 20, type="random") %>% st_sf
#Grid 50km x 50km
grid_50 <- st_make_grid(uso, cellsize = c(5000, 5000)) %>%
st_sf(grid_id = 1:length(.))
#Labels grid
grid_lab <- st_centroid(grid_50) %>% cbind(st_coordinates(.))
#Points in grid
pts %>% st_join(grid_50, join = st_intersects) %>% as.data.frame
#Map --------------------------------------------------------------------
ggplot() +
geom_sf(data = uso, aes(fill = CLASSE_USO, color = NA)) +
geom_sf(data = rio_claro_limit, aes(color = 'black', fill = NA)) +
geom_sf(data = pts, aes(color = 'red'), size = 1.7) +
geom_sf(data = grid_50, aes(fill=NA), lwd = 0.3) +
geom_text(data = grid_lab, aes(x = X, y = Y, label = grid_id), size = 2) +
xlab("")+
ylab("")+
scale_fill_manual(name="Classes de Uso", values = c("blue", "orange", "gray30", "forestgreen", "green", NA))+
scale_color_identity(guide = "legend")


shapeof the markers, despitecolorandfill. Only placecolorandfillinside theaeswhen you want it to show up in the legend for the that particular geometry. The borders might be related to thataesspecification. The grey background of the legend items can be removed by mentioninglegend.key = element_blank()in thetheme.