2013-03-18 124 views
3

用於繪製比例尺和指北針在地圖上用GGPLOT2比例尺和指北針在地圖GGPLOT2

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

感謝您的時間的任何解決方案。

回答

5

幾年前,我製作了一些可以繪製比例尺的代碼(另請參閱this post on r-sig-geo),這是我當時編寫的代碼。你可以給它一個去:

首先是一些支持功能:

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))) 
} 

而真正的功能:

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)) 
} 

而且一些示例代碼:

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)) 
+0

'addScaleBar'產生錯誤:geom_XXX和stat_XXX中的'「legend」參數已被棄用。使用show_guide = TRUE或show_guide = FALSE進行顯示或禁止顯示指南。 (Defunct;最後在0.8.9版本中使用)'我能做什麼? – 2015-04-24 13:21:18