trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
359 stars 18 forks source link

Overlaying a raster with hatchings on top of another raster map #33

Closed JEFFGAFNA16 closed 3 years ago

JEFFGAFNA16 commented 3 years ago

This is actually not an issue but am sure it will be helpful to many others in future. I have been struggling, for the past months, to plot this using ggpattern package. I asked the question on ggplot2 google group and Ecology in R facebook group but did not get conclusive replies. I have two rasters, one is a continuous species richness raster and the other is a binary raster of a single species called Olea Africana. I would like to plot the species richness map (with legend) and then on top of it the Olea african map with transparent hatchings (also with a legend). Am really confused on how to overlay the Olea africana map but my plots using ggplot2 for the two are very nice. I would really appreciate any help.

Here are the links to my two rasters and the code

Links to rasters

https://www.dropbox.com/s/x763qz7m0qn97e9/final_masked_all_richness.tif?dl=0

https://www.dropbox.com/s/w697cmyj3ihmvjs/Olea_africana.tif?dl=0

Code

library(raster) library(ggplot2) library(tidyverse) library(viridis) library(see) library(ggpattern)

read the overal reachness map

richness <- raster("Outputs/all_final_rasters/species/final_masked_all_richness.tif") plot(richness)

convert to a df for plotting in two steps,

First, to a SpatialPointsDataFrame

mal_ras_pts <- rasterToPoints(richness, spatial = TRUE)

Then to a 'conventional' dataframe

mal_ras_df <- data.frame(mal_ras_pts) rm(mal_ras_pts, richness)

names(mal_ras_df)

species_richness_df <- mal_ras_df %>% mutate(fct_my_richness = cut(final_masked_all_richness, breaks = 6))

names(species_richness_df)

plot using ggplot

ggplot() + geom_raster(data = species_richness_df , aes(x = x, y = y, fill = fct_my_richness)) + scale_fill_manual(values = rainbow(6), name = "Richness", labels = c("4-5", "6-7", "8-9", "10-11", "12-13", "14-15")) + ggtitle("Current Richness") + theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + theme(plot.title=element_text(size=10)) + theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + coord_quickmap() + theme(legend.position = c(0.71, 0.83))

Read Olea africana binary raster

olea <- raster("Outputs/all_final_rasters/Current_species/masked_binary_current_species/Olea_africana.tif") plot(olea)

convert to a df for plotting in two steps,

First, to a SpatialPointsDataFrame

olea_pts <- rasterToPoints(olea, spatial = TRUE)

Then to a 'conventional' dataframe

olea_df <- data.frame(olea_pts) rm(olea_pts, olea)

names(olea_df)

ggplot() + geom_raster(data = olea_df , aes(x = x, y = y, fill = as.factor(Olea_africana), alpha = 0.2)) + scale_alpha(guide = 'none') + scale_color_manual(values=c('#999999','#E69F00')) + theme(legend.title = element_text(color = "black", size = 8), legend.text = element_text(color = "black")) + ggtitle("Olea africana") + labs(fill = "Habitat Suitability") + scale_fill_discrete(labels = c("No Habitat", "Olea africana")) + theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + theme(plot.title=element_text(size=10)) + theme(axis.title.x = element_blank(), axis.title.y = element_blank()) + coord_quickmap() + theme(legend.position = c(0.82, 0.53))

Using cross hatchings to overlay transparent olea africana map (with hatchings) on top of the species richness map