Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 如何使用levelplot或spplot生成状态形状文件和空间点?_R_Ggplot2_Raster_Levelplot - Fatal编程技术网

R 如何使用levelplot或spplot生成状态形状文件和空间点?

R 如何使用levelplot或spplot生成状态形状文件和空间点?,r,ggplot2,raster,levelplot,R,Ggplot2,Raster,Levelplot,我有一个光栅图像,我想覆盖夏威夷州的形状文件,为地图提供更大的背景。另外,我想在完成的地图上画出具体的点。目前,我有一个夏威夷形状文件的ggplot图像,其中绘制了点: df <- data.frame(lon, lat) ggplot(data = world) + geom_sf() + coord_sf(xlim = c(-160, -150), ylim = c(10, 25)) + geom_point(data = df, mapping = a

我有一个光栅图像,我想覆盖夏威夷州的形状文件,为地图提供更大的背景。另外,我想在完成的地图上画出具体的点。目前,我有一个夏威夷形状文件的
ggplot
图像,其中绘制了点:

df <- data.frame(lon, lat)

ggplot(data = world) + geom_sf() + 
       coord_sf(xlim = c(-160, -150), ylim = c(10, 25)) +
       geom_point(data = df, mapping = aes(x = lon, y = lat) +
       theme()
当我尝试:

> ggplot(data = world) + geom_sf() + coord_sf(xlim = c(-158, -154), ylim = c(17, 24), expand = FALSE) + levelplot(b)
我得到这个信息:

Error in ggplot(data = world) + geom_sf() + coord_sf(xlim = c(-158, -154),  : 
  non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.gg", "+.trellis") for "+" 
如果这些映射类型不兼容,我如何通过
levelplot
spplot
使用空间点生成夏威夷形状文件?我意识到使用可复制的Rstudio光栅时,这些数据很奇怪,但我想让这个问题可以复制


提前感谢您的指导

您可以从网站下载夏威夷的shapefile。然后,您可以使用以下代码绘制它

library(sf)
library(ggplot2)
library(rgdal)
library(rgeos)

#Reading the shapefiles
sf <- st_read(dsn="C:\\Users\\User\\Desktop\\cty_council_dist.shp", layer="cty_council_dist_haw")
shape <- readOGR(dsn="C:\\Users\\User\\Desktop\\cty_council_dist.shp", layer="cty_council_dist_haw")

#To view the attributes
head(shape@data)
summary(sf)

#Plotting the shapefile
plot(shape)
plot(sf)

#Covert the coordinate system
sf_gcs <- st_transform(sf, crs = "EPSG:4326")
shape_gcs <- spTransform(shape, CRS=CRS("+init=EPSG:4326"))

#Plotting the shapefile
plot(shape)
plot(sf_gcs)

#Plotting the districts only
plot(sf_gcs["cntydist"], axes = TRUE, main = "Districts")
#Load the libraries
library(rasterVis)
library(RColorBrewer)

## Create a matrix with random data & use image()
xy <- matrix(rnorm(400),20,20)
image(xy)

# Turn the matrix into a raster
rast <- raster(xy)

# Give it lat/lon coords for 156.2-154.8°W, 18.5-20.5°N
extent(rast) <- c(-156.2, -154.8, 18.5, 20.5)

#Assign a projection
projection(rast) <- CRS("+proj=longlat +datum=WGS84")
plot(rast)

colr <- colorRampPalette(brewer.pal(11, 'RdYlBu'))

#Plotting Using ggplot2
gplot(rast) +  
  geom_tile(aes(fill=factor(value),alpha=0.8)) + 
  geom_polygon(data=shape_gcs, aes(x=long, y=lat, group=group),
               fill=NA,color="grey50", size=1) + 
  theme(legend.position = "none")
更新

要使用
ggplot2
打印光栅,可以使用以下代码

library(sf)
library(ggplot2)
library(rgdal)
library(rgeos)

#Reading the shapefiles
sf <- st_read(dsn="C:\\Users\\User\\Desktop\\cty_council_dist.shp", layer="cty_council_dist_haw")
shape <- readOGR(dsn="C:\\Users\\User\\Desktop\\cty_council_dist.shp", layer="cty_council_dist_haw")

#To view the attributes
head(shape@data)
summary(sf)

#Plotting the shapefile
plot(shape)
plot(sf)

#Covert the coordinate system
sf_gcs <- st_transform(sf, crs = "EPSG:4326")
shape_gcs <- spTransform(shape, CRS=CRS("+init=EPSG:4326"))

#Plotting the shapefile
plot(shape)
plot(sf_gcs)

#Plotting the districts only
plot(sf_gcs["cntydist"], axes = TRUE, main = "Districts")
#Load the libraries
library(rasterVis)
library(RColorBrewer)

## Create a matrix with random data & use image()
xy <- matrix(rnorm(400),20,20)
image(xy)

# Turn the matrix into a raster
rast <- raster(xy)

# Give it lat/lon coords for 156.2-154.8°W, 18.5-20.5°N
extent(rast) <- c(-156.2, -154.8, 18.5, 20.5)

#Assign a projection
projection(rast) <- CRS("+proj=longlat +datum=WGS84")
plot(rast)

colr <- colorRampPalette(brewer.pal(11, 'RdYlBu'))

#Plotting Using ggplot2
gplot(rast) +  
  geom_tile(aes(fill=factor(value),alpha=0.8)) + 
  geom_polygon(data=shape_gcs, aes(x=long, y=lat, group=group),
               fill=NA,color="grey50", size=1) + 
  theme(legend.position = "none")

您可以访问和。感谢您提供的链接和详细回复,但它们没有解决我的主要问题。我用levelplot绘制了一个光栅。ggplot与levelplot不兼容。我想知道是否可以使用levelplot或spplot生成一个shapefile。
#Load the libraries
library(rasterVis)
library(RColorBrewer)

## Create a matrix with random data & use image()
xy <- matrix(rnorm(400),20,20)
image(xy)

# Turn the matrix into a raster
rast <- raster(xy)

# Give it lat/lon coords for 156.2-154.8°W, 18.5-20.5°N
extent(rast) <- c(-156.2, -154.8, 18.5, 20.5)

#Assign a projection
projection(rast) <- CRS("+proj=longlat +datum=WGS84")
plot(rast)

colr <- colorRampPalette(brewer.pal(11, 'RdYlBu'))

#Plotting Using ggplot2
gplot(rast) +  
  geom_tile(aes(fill=factor(value),alpha=0.8)) + 
  geom_polygon(data=shape_gcs, aes(x=long, y=lat, group=group),
               fill=NA,color="grey50", size=1) + 
  theme(legend.position = "none")
levelplot(rast, 
          margin=F,                       # suppress marginal graphics
          colorkey=list(
            space='right'                   # plot legend at right
          ),    
          par.settings=list(
            axis.line=list(col='transparent') # suppress axes and legend outline
          ),
          scales=list(draw=FALSE),            # suppress axis labels
          col.regions=colr,                   # colour ramp
          at=seq(-5, 5, len=101)) +           # colour ramp breaks
  layer(sp.polygons(shape_gcs, lwd=1))           # add shapefile with latticeExtra::layer