本文介绍了在地图-ggplot2上的比例尺和北向箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot2绘制比例尺和北向箭头的任何解决方案

  library(mapdata);库(GGPLOT2); 
nl.map = data.frame(map('worldHires','Netherlands')[c('x','y')])
ggplot(nl.map,aes(x,y ))+ geom_path()

感谢您的宝贵时间。

sig-geo / 2010-December / 010382.htmlrel =noreferrer> r-sig-geo上的这篇文章),这是我当时写的代码。您可以试试看:



首先支持一些功能:

 #由Maarten Plieger提供的代码四舍五入
return )*(10 ^(round(log10(num)) - 1))))
}

createBoxPolygon = function(llcorner,width,height){
relativeCoords = data .frame(c(0,0,width,width,0),c(0,height,height,0,0))
names(relativeCoords)= names(llcorner)
return(t apply(relativeCoords,1,function(x)llcorner + x)))
}

和真正的函数:

pre $ addCaleBar = function(ggplot_obj,spatial_obj,attribute,addParams =
list()){
addParamsDefaults = list(noBins = 5,xname =x,yname =y,unit =m,
placement =bottomright,sbLengthPct = 0.3,sbHeightvsWidth = 1/14)
addParams = modifyList(addParamsDefaults,addParams)

range_x = max (spatial_obj [[addParams [[xname]]]]) - min(spatial_obj [[addParams [[xname]]]])
range_y = max(spatial_obj [[addParams [[yname]] ]]]) - min(spatial_obj [[addParams [[yname]]]])
lengthScalebar = addParams [[sbLengthPct]] * range_x
##选项:使用pretty()
widthBin = makeNiceNumber(lengthScalebar / addParams [[noBins]])
heightBin = lengthScalebar * addParams [[sbHeightvsWidth]]
lowerLeftCornerScaleBar = c(x = max(spatial_obj [ addParams [[xname]]]]) - (widthBin * addParams [[noBins]]),y = min(spatial_obj [[addParams [[yname]]]))
scaleBarPolygon =函数(n){
dum = data.frame(createBoxPolygon(lowerLeftCornerScaleBar + c((n * widthBin) ),0),widthBin,heightBin))
if(!(n + 1)%% 2 == 0)dum $ cat =oddelse dum $ cat =even
return dum)
)))
scaleBarPolygon [[attribute]] = min(spatial_obj [[attribute]])
textScaleBar = d ata.frame(x = lowerLeftCornerScaleBar [[addParams [[xname]]]] +(c(0:(addParams [[noBins]]))* widthBin),y = lowerLeftCornerScaleBar [[addParams [[yname ]]]],
label = as.character(0:(addParams [[noBins]])* widthBin))
textScaleBar [[attribute]] = min(spatial_obj [[attribute] ])

return(ggplot_obj +
geom_polygon(data = subset(scaleBarPolygon,cat ==odd),fill =black,color =black,legend = FALSE) +
geom_polygon(data = subset(scaleBarPolygon,cat ==even),fill =white,color =black,legend = FALSE)+
geom_text(aes(label = label) ,color =black,size = 6,data = textScaleBar,hjust = 0.5,vjust = 1.2,legend = FALSE))
}

以及一些示例代码:

  library(ggplot2)
library (sp)

data(meuse)
data(meuse.grid)
ggobj = ggplot(aes(x = x,y = y,color = zinc) meuse)+ geom_point()
#确保进入折叠图形设备
addScaleBar(ggobj,meuse,zinc,addParams = list(noBins = 5))


Any solution for drawing a scale bar and north arrow on map with ggplot2

library(mapdata); library(ggplot2); 
nl.map=data.frame(map('worldHires', 'Netherlands')[c('x', 'y')])
ggplot(nl.map, aes(x, y))+geom_path()

Thanks for your time.

解决方案

A few years back I produced some code that could draw a scalebar (see also this post on r-sig-geo), this is the code I wrote back then. You could give it a go:

First some support functions:

makeNiceNumber = function(num, num.pretty = 1) {
   # Rounding provided by code from Maarten Plieger
   return((round(num/10^(round(log10(num))-1))*(10^(round(log10(num))-1))))
}

createBoxPolygon = function(llcorner, width, height) {
   relativeCoords = data.frame(c(0, 0, width, width, 0), c(0, height, height, 0, 0))
   names(relativeCoords) = names(llcorner)
   return(t(apply(relativeCoords, 1, function(x) llcorner + x)))
}

And the real function:

addScaleBar = function(ggplot_obj, spatial_obj, attribute, addParams = 
list()) {
   addParamsDefaults = list(noBins = 5, xname = "x", yname = "y", unit = "m", 
        placement = "bottomright", sbLengthPct = 0.3, sbHeightvsWidth = 1/14)
   addParams = modifyList(addParamsDefaults, addParams)

   range_x = max(spatial_obj[[addParams[["xname"]]]]) - min(spatial_obj[[addParams[["xname"]]]])
   range_y = max(spatial_obj[[addParams[["yname"]]]]) -  min(spatial_obj[[addParams[["yname"]]]])
   lengthScalebar = addParams[["sbLengthPct"]] * range_x
   ## OPTION: use pretty() instead
   widthBin = makeNiceNumber(lengthScalebar / addParams[["noBins"]])
   heightBin = lengthScalebar * addParams[["sbHeightvsWidth"]]
   lowerLeftCornerScaleBar = c(x = max(spatial_obj[[addParams[["xname"]]]]) - (widthBin * addParams[["noBins"]]), y = min(spatial_obj[[addParams[["yname"]]]]))
   scaleBarPolygon = do.call("rbind", lapply(0:(addParams[["noBins"]] - 1), function(n) {
     dum = data.frame(createBoxPolygon(lowerLeftCornerScaleBar + c((n * widthBin), 0), widthBin, heightBin))
     if(!(n + 1) %% 2 == 0) dum$cat = "odd" else dum$cat = "even"
     return(dum)
   }))
   scaleBarPolygon[[attribute]] = min(spatial_obj[[attribute]])
   textScaleBar = data.frame(x = lowerLeftCornerScaleBar[[addParams[["xname"]]]] + (c(0:(addParams[["noBins"]])) * widthBin), y = lowerLeftCornerScaleBar[[addParams[["yname"]]]],
                             label = as.character(0:(addParams[["noBins"]]) * widthBin))
   textScaleBar[[attribute]] = min(spatial_obj[[attribute]])

   return(ggplot_obj +
     geom_polygon(data = subset(scaleBarPolygon, cat == "odd"), fill = "black", color = "black", legend = FALSE) +
     geom_polygon(data = subset(scaleBarPolygon, cat == "even"), fill = "white", color = "black", legend = FALSE) +
     geom_text(aes(label = label), color = "black", size = 6, data = textScaleBar, hjust = 0.5, vjust = 1.2, legend = FALSE))
}

And some example code:

library(ggplot2)
library(sp)

data(meuse)
data(meuse.grid)
ggobj = ggplot(aes(x = x, y = y, color = zinc), data = meuse) + geom_point()
# Make sure to increase the graphic device a bit
addScaleBar(ggobj, meuse, "zinc", addParams = list(noBins = 5))

这篇关于在地图-ggplot2上的比例尺和北向箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:46