R 如何在地图上覆盖透明光栅?
我正试图找到一种方法来添加R 如何在地图上覆盖透明光栅?,r,ggplot2,raster,ggmap,r-raster,R,Ggplot2,Raster,Ggmap,R Raster,我正试图找到一种方法来添加alpha函数,以便在ggmap上覆盖光栅层。有一些建议,但我想使用光栅文件。有没有办法添加alpha并使光栅层透明 library(raster) library(ggmap) location <- get_map(location = c(lon =22, lat =40), zoom = 6, maptype = "hybrid") # get the raster layer tmin <- raster::getData('worldclim',
alpha
函数,以便在ggmap
上覆盖光栅层
。有一些建议,但我想使用光栅文件
。有没有办法添加alpha
并使光栅
层透明
library(raster)
library(ggmap)
location <- get_map(location = c(lon =22, lat =40), zoom = 6, maptype = "hybrid")
# get the raster layer
tmin <- raster::getData('worldclim', var='tmin', res=0.5, lon=22, lat=40)[[1]]
ggmap(location)+inset_raster(as.raster(tmin), xmin = tmin@extent[1], xmax = tmin@extent[2],ymin =tmin@extent[3], ymax = tmin@extent[4])+ coord_cartesian()
库(光栅)
图书馆(ggmap)
location不久前,我修改了rasterVis::gplot
函数,以使用经典的{ggplot2}函数(如geom_tile
)检索要绘制的数据。函数是my中的gplot\u数据
。如果您不想获得完整的软件包,可以直接使用以下功能:
#' Transform raster as data.frame to be later used with ggplot
#' Modified from rasterVis::gplot
#'
#' @param x A Raster* object
#' @param maxpixels Maximum number of pixels to use
#'
#' @details rasterVis::gplot is nice to plot a raster in a ggplot but
#' if you want to plot different rasters on the same plot, you are stuck.
#' If you want to add other information or transform your raster as a
#' category raster, you can not do it. With `SDMSelect::gplot_data`, you retrieve your
#' raster as a data.frame that can be modified as wanted using `dplyr` and
#' then plot in `ggplot` using `geom_tile`.
#' If Raster has levels, they will be joined to the final tibble.
#'
#' @export
gplot_data <- function(x, maxpixels = 50000) {
x <- raster::sampleRegular(x, maxpixels, asRaster = TRUE)
coords <- raster::xyFromCell(x, seq_len(raster::ncell(x)))
## Extract values
dat <- utils::stack(as.data.frame(raster::getValues(x)))
names(dat) <- c('value', 'variable')
dat <- dplyr::as.tbl(data.frame(coords, dat))
if (!is.null(levels(x))) {
dat <- dplyr::left_join(dat, levels(x)[[1]],
by = c("value" = "ID"))
}
dat
}
不久前,我修改了rasterVis::gplot
函数,以检索使用经典的{ggplot2}函数(如geom\u tile
绘制的数据。函数是my中的gplot\u数据
。如果您不想获得完整的软件包,可以直接使用以下功能:
#' Transform raster as data.frame to be later used with ggplot
#' Modified from rasterVis::gplot
#'
#' @param x A Raster* object
#' @param maxpixels Maximum number of pixels to use
#'
#' @details rasterVis::gplot is nice to plot a raster in a ggplot but
#' if you want to plot different rasters on the same plot, you are stuck.
#' If you want to add other information or transform your raster as a
#' category raster, you can not do it. With `SDMSelect::gplot_data`, you retrieve your
#' raster as a data.frame that can be modified as wanted using `dplyr` and
#' then plot in `ggplot` using `geom_tile`.
#' If Raster has levels, they will be joined to the final tibble.
#'
#' @export
gplot_data <- function(x, maxpixels = 50000) {
x <- raster::sampleRegular(x, maxpixels, asRaster = TRUE)
coords <- raster::xyFromCell(x, seq_len(raster::ncell(x)))
## Extract values
dat <- utils::stack(as.data.frame(raster::getValues(x)))
names(dat) <- c('value', 'variable')
dat <- dplyr::as.tbl(data.frame(coords, dat))
if (!is.null(levels(x))) {
dat <- dplyr::left_join(dat, levels(x)[[1]],
by = c("value" = "ID"))
}
dat
}