lattice levelplot 或 ggplot2 map R 的自定义图例
Posted
技术标签:
【中文标题】lattice levelplot 或 ggplot2 map R 的自定义图例【英文标题】:Custom legend for lattice levelplot or ggplot2 map R 【发布时间】:2018-07-29 01:29:09 【问题描述】:鉴于以下code
、map
和sample data:
而不是在地图上绘制watersheds
等的名称,如何使用ggplot2
或levelplot
生成单独的图例,就像使用ArcGIS?
一样基本上,我想生成这张地图上显示的图例最好是levelplot
或ggplot2
。
Code
和 map
以及用于在R
中绘图的示例数据。
require(colorRamps)
require(raster)
require(rasterVis)
require(mapproj)
library(raster)
library(proj4)
# Get province borders and project it to same CRS than raster
can1 <- getData('GADM', country="CAN", level=1)
getData('ISO3') # country name
dem=getData('alt', country='CAN', mask=TRUE)
require( colorRamps )
my.at <- unique(round(seq(ceiling(5800), floor(1), length.out = 51),0))#at: numeric vector specifying where the colors change. must be of length 1 more than the col vector.
myColorkey <- list(at=my.at,space = "bottom", tck = c(0,0),## where the colors change
labels=list(axis.line = list(col = NA),at=myat1,rot=0,cex=1.4,font=1,
fontface=1),height=0.99,width=1.8)
col<-colorRampPalette(c("#d9d9d9", "#bdbdbd", "#969696", "#737373", "#525252", "#252525", "#000000"))
levelplot(dem,maxpixel=ncell(dem),panel=panel.levelplot.raster,names.attr=names(dem),
interpolate=F,margin=FALSE,xlab=list(label="Longitude",cex=1.5),yscale.components = yscale.raster.subticks,
xscale.components = xscale.raster.subticks,
ylab=list(label="Latitude",cex=1.5),
par.strip.text=list(cex=1),xlim=c(-158, -48.99485),
ylim=c(38.00513, 85),col.regions=col,at = seq(0, 5800,100),
colorkey = list(space = "bottom", labels = list(at = seq(0, 5800,800), rot=0,cex=1.1,font=6,fontface=1,
labels =c("0", "800", "1600", "2400", "3200", "4000", "4800", "5600")),
height=0.99,width=1.8,tck = c(0,0)),
par.settings=list(panel.background=list(col="white"),axis.line=list(lwd=1.9), strip.border=list(lwd=1.9),layout.heights=list(xlab.key.padding=-0.2)),
cex=0.8, scales = list(x=list(draw=TRUE,cex=1.2), y=list(draw=TRUE,cex=1.2)))+
layer(sp.polygons(can1,lwd=0.5,col="gray40"))+
layer(sp.polygons(watershed,lwd=1,col=c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00"),
fill=c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00")))+
layer(sp.polygons(lake,lwd=1,col="cadetblue1",fill="cadetblue1"))+
layer(sp.lines(river,lwd=0.5,col="blue4"))+
layer(sp.text(coordinates(watershed), txt = watershed$NAMRB_EN, pos =c(1,1,3,3,1,4,1,1),col="black",font=list(face="bold"),cex=0.8))
【问题讨论】:
myat1
未分配。
地图的用途是什么?当我对高程感兴趣时,我会尝试摆脱填充颜色的多边形,并仅绘制每个分水岭的边界。例如,ArcGIS 地图中的哥伦比亚分水岭完全破坏了高程信息。
【参考方案1】:
您可以尝试将您的代码调整为this example,以便为分水岭和高程添加图例:
library(sp)
library(raster)
library(rasterVis)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
data(meuse)
coordinates(meuse) = c("x", "y")
meuse$class <- cut(meuse$zinc, 3)
ptsCols <- c('black', 'red', 'blue')
lvp <- levelplot(r, margin = FALSE,
## Define the legend for the spplot graphic
key = list(space = 'top', text = list(levels(meuse$class)),
points = list(pch = 21, fill = ptsCols)),
## Legend for the Raster object
colorkey=list(space="right",width=0.75,height=0.75))
spp <- spplot(meuse["class"], col.regions = ptsCols)
lvp + spp
【讨论】:
谢谢@Christpher。传说中的湖泊河流呢?以上是关于lattice levelplot 或 ggplot2 map R 的自定义图例的主要内容,如果未能解决你的问题,请参考以下文章
我怎样才能问一个很棒的 ggplot / lattice / base plot 问题?
我可以使用 Lattice auto.key 或 key 来制作带有点的图例和其他数据的线吗?