I try to do this ggplot:
library (sf)
library(dplyr)
library(ggplot2)
continents.f.xy <- st_zm(continents.f)
continents_map_SA <- continents.f.xy%>%
dplyr::bind_rows() %>% #bind the list into a data.frame
sf::st_cast(to = "POINT") %>% #convert polygon to a list of points
dplyr::mutate(
long = sf::st_coordinates(geometry)[,1], #retrieve X coord
lat = sf::st_coordinates(geometry)[,2], #retrieve Y coord
group = rep(1, nrow(.)) #create a group column
) %>%
sf::st_drop_geometry() #drop the geometry column
continents_map_SA <- continents_map_SA%>%select(long,lat,group)
continents_map_SA <- fortify(continents_map_SA , region = "South America")
plotactual.high<-ggplot() +
geom_raster(data = ras.actual.df, aes(x = longitude, y = latitude, fill = hpds)) +
scale_fill_gradientn(name="hpds",colours = c("white","red"))+
#scale_y_continuous(breaks=seq(-50, 50, by = 10)) +
#scale_x_continuous(breaks=seq(-100, 0, by = 10)) +
xlab("longitude") + ylab("latitude") +
geom_polygon(data = continents_map_SA, aes(x=long, y = lat, group = group), fill = NA, color = "black") +
annotate(geom="text", x=-45, y=-45, label="Potential area = 9,338.380 km2", color="black", family = c("serif"), size=4.5) +
coord_quickmap() +
theme_bw() +
theme(panel.border = element_blank(), plot.margin = margin(r = 1, l = 1), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
text=element_text(size=16, family="serif"),legend.position="none")
plotactual.high
and I have as a result:
I also use fortify
and group
inside aes
in geom_polygon
and arrange
.