Skip to content

Commit

Permalink
update plotRaster to work with spatRaster objects
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomflavio committed Dec 25, 2023
1 parent 24d5b51 commit b03eab6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Imports:
stringr,
sf,
terra,
tidyterra,
tidyr,
utils,
reshape2,
Expand Down
21 changes: 7 additions & 14 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -985,19 +985,11 @@ plotRaster <- function(input, base.raster, coord.x, coord.y, size = 1, land.col
stop("Please indicate the latitude column with 'coord.y'.\n", call. = FALSE)

# paint land rather than water
base.raster[is.na(base.raster)] <- 2
base.raster[base.raster == 1] <- NA
base.raster[base.raster == 2] <- 1
base.raster <- terra::as.factor(water)
base.raster <- terra::subst(base.raster, from = 1, to = NA, others = 1)

# Convert raster to points:
base.raster_df <- raster::rasterToPoints(base.raster)

# Make the points a dataframe for ggplot
df <- data.frame(base.raster_df)
colnames(df) <- c("Longitude", "Latitude", "MAP")

# Find stations in land:

# Find stations in land:
if (any(names(input) == "rsp.info"))
stations <- input$spatial$stations
else
Expand All @@ -1011,10 +1003,10 @@ plotRaster <- function(input, base.raster, coord.x, coord.y, size = 1, land.col

if (is.na(match(coord.y, colnames(stations))))
stop("Could not find column '", coord.y, "' in the spatial data frame", call. = FALSE)

on.land <- raster::extract(x = base.raster, y = as.matrix(stations[, c(coord.x, coord.y)]))

data.stations <- data.frame(Check = on.land,
data.stations <- data.frame(Check = as.character(on.land[,1]),
Longitude = stations[, coord.x],
Latitude = stations[, coord.y])
data.stations$Check[is.na(data.stations$Check)] <- "Water"
Expand All @@ -1024,7 +1016,8 @@ plotRaster <- function(input, base.raster, coord.x, coord.y, size = 1, land.col
legend_labels <- c(paste0("On land (", sum(data.stations$Check == "Land"), ")"), paste0("On water (", sum(data.stations$Check == "Water"), ")"))

p <- ggplot2::ggplot()
p <- p + ggplot2::geom_raster(data = df, ggplot2::aes(y = Latitude, x = Longitude), fill = land.col, show.legend = FALSE)
p <- p + tidyterra::geom_spatraster(data = base.raster, ggplot2::aes(fill = layer), show.legend = FALSE)
p <- p + ggplot2::scale_fill_manual(values = land.col, na.value = "transparent")
p <- p + ggplot2::theme_bw()
p <- p + ggplot2::theme(legend.position = "bottom")
p <- p + ggplot2::scale_x_continuous(expand = c(0, 0))
Expand Down

0 comments on commit b03eab6

Please sign in to comment.