【问题标题】:What is the "Simplest" way to add a scale to a map in ggmap在ggmap中向地图添加比例的“最简单”方法是什么
【发布时间】:2016-01-26 04:54:38
【问题描述】:

说真的,我到处搜索,在地图上编写比例尺太难了…… Adding scale bar to ggplot map

Is there a way to add a scale bar (for linear distances) to ggmap?

是否可以制作一条与我们在函数中选择的缩放预设不同缩放的简单线条?

我有这张简单的地图:

library(ggmap)
pngMAP_df2 = get_map(location = c(-90.5, -0.5), source = "google", zoom = 8,color = "bw")
s = ggmap(pngMAP_df2)
s

我还想在此图中添加 GPS 坐标:

myGPS = data.frame(lat=c( -0.6850556,-0.6854722,  -0.6857778  ),lon=c(-90.22275,-90.22261,  -90.22272)) 

容易实现吗?

我只想添加一些非常简单的东西。就像一条始终为整数的线,表示地图的缩放。

此外,是否可以使用此代码使地图看起来更简单。就像看到白色的水和黑色的陆地轮廓?

谢谢,

【问题讨论】:

    标签: r google-maps maps ggmap


    【解决方案1】:

    类似:

    library(rgdal)
    library(rgeos)
    library(ggplot2)
    library(ggthemes)
    library(ggsn)
    
    URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
    fil <- "gal.json"
    if (!file.exists(fil)) download.file(URL, fil)
    
    gal <- readOGR(fil, "OGRGeoJSON")
    
    # sample some points BEFORE we convert gal
    rand_pts <- SpatialPointsDataFrame(spsample(gal, 100, type="random"), data=data.frame(id=1:100))
    
    gal <- gSimplify(gUnaryUnion(spTransform(gal, CRS("+init=epsg:31983")), id=NULL), tol=0.001)
    
    gal_map <- fortify(gal)
    
    # now convert our points to the new CRS
    rand_pts <- spTransform(rand_pts, CRS("+init=epsg:31983"))
    
    # and make it something ggplot can use
    rand_pts_df <- as.data.frame(rand_pts)
    
    gg <- ggplot()
    gg <- gg + geom_map(map=gal_map, data=gal_map,
                        aes(x=long, y=lat, map_id=id),
                        color="black", fill="#7f7f7f", size=0.25)
    gg <- gg + geom_point(data=rand_pts_df, aes(x=x, y=y), color="steelblue")
    gg <- gg + coord_equal()
    gg <- gg + scalebar(gal_map, dist=100, location="topright", st.size=2)
    gg <- gg + theme_map()
    gg
    

    【讨论】:

    • 昨天还在工作。现在我有这个错误...Error in FUN(left, right) : non-numeric argument to binary operator In addition: Warning messages: 1: In Ops.factor(left, right) : ‘*’ not meaningful for factors 2: In Ops.factor(left, right) : ‘*’ not meaningful for factors
    • 好的,我找到了。我只需要更正这一行:gg &lt;- gg + ggsn:::scalebar(gal_map, dist=100, location="topright", st.size=2) 这是因为我的包栅格处于活动状态。这与 ggsn 冲突
    • 由于这个新图表不支持小数点,我如何向图表添加点?
    • 我通常会在谷歌上搜索某种形式的区域名称并添加“shapefile”,但我经常需要在我的谷歌搜索中获得非常有创意的内容。
    • 非常感谢让我发现这段代码!我现在可以自己做。真的很有帮助。
    【解决方案2】:

    这将是地图上特定点的完整答案。

    library(rgdal)
    library(rgeos)
    library(ggplot2)
    library(ggthemes)
    library(ggsn)
    myGPS = data.frame(lat=c( -0.6850556,-0.6854722,  -0.6857778  ),lon=c(-90.22275,-90.22261,  -90.22272)) 
    coord.deg = myGPS
    
    class(coord.deg)
    ## "data.frame"
    coordinates(coord.deg)<-~lon+lat
    class(coord.deg)
    ## "SpatialPointsDataFrame"
    ## attr(,"package")
    ## "sp"
    
    # does it have a projection/coordinate system assigned?
    proj4string(coord.deg) # nope
    ## NA
    
    # Manually tell R what the coordinate system is
    proj4string(coord.deg)<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
    
    # now we can use the spTransform function to project. We will project
    # the mapdata and for coordinate reference system (CRS) we will
    # assign the projection from counties
    
    coord.deg<-spTransform(coord.deg, CRS(proj4string(gal)))
    # double check that they match
    identical(proj4string(coord.deg),proj4string(gal))
    ## [1] TRUE
    my_pts <- SpatialPointsDataFrame(coords = coord.deg, data=data.frame(id=1:length(coord.deg)))
    
    URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
    fil <- "gal.json"
    if (!file.exists(fil)) download.file(URL, fil)
    gal <- readOGR(fil, "OGRGeoJSON")
    gal <- gSimplify(gUnaryUnion(spTransform(gal, CRS("+init=epsg:31983")), id=NULL), tol=0.001)
    gal_map <- fortify(gal)
    rand_pts <- spTransform(my_pts, CRS("+init=epsg:31983"))
    
    # ggplot can't deal with a SpatialPointsDataFrame so we can convert back to a data.frame
    my_pts <- data.frame(my_pts)
    my_pts.final = my_pts[,2:3]
    # we're not dealing with lat/long but with x/y
    # this is not necessary but for clarity change variable names
    names(my_pts.final)[names(my_pts.final)=="lat"]<-"y"
    names(my_pts.final)[names(my_pts.final)=="lon"]<-"x"
    
    gg <- ggplot()
    gg <- gg + geom_map(map=gal_map, data=gal_map,
                        aes(x=long, y=lat, map_id=id),
                        color="black", fill="#FFFFFF", size=.5)
    gg <- gg + coord_equal()
    gg <- gg + ggsn:::scalebar(gal_map, dist=50, location="bottomleft", st.size=5)
    gg <- gg + theme_map()
    gg <- gg + geom_point(data=my_pts.final, aes(x=x, y=y), color="red")
    gg
    

    【讨论】:

      猜你喜欢
      • 2020-07-28
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 2010-09-12
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      相关资源
      最近更新 更多