Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在地图上覆盖透明光栅?_R_Ggplot2_Raster_Ggmap_R Raster - Fatal编程技术网

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
}