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