【问题标题】:Plot Pacific Ocean and continents with ggplot2::borders()用 ggplot2::borders() 绘制太平洋和大陆
【发布时间】:2015-12-01 01:29:35
【问题描述】:

我想用ggplot::borders() 显示太平洋。我的问题是我无法弄清楚如何向西展示澳大利亚和亚洲,向东展示美洲。

ggplot() + borders() + coord_cartesian(xlim = c(-290, -70))

在结果图中,应该显示亚洲和澳大利亚,但实际上没有。

我想做一些类似的事情:

ggplot() + borders() + borders(x = long - 360)

得到这样的地图:

谁能帮我解决这个问题?对于mapsggmap 软件包,SO 上有答案,但我无法使用ggplot2::borders() 找到答案。

任何答案的一个重要方面是经纬度值是正确的。挑战在于:使用传统的经度坐标,太平洋的视图将从增加的正值(E 半球)开始,然后在逆子午线(W 半球)切换到减少的负值。我怎样才能在 R 中最好地在borders() 中绘制这个?

【问题讨论】:

    标签: r ggplot2 maps


    【解决方案1】:

    我已尝试将其放大一点以更接近您发布的内容(代码如下)。感谢this answerkohske

    # install.packages("mapdata", dependencies = TRUE)
    # install.packages("ggplot2", dependencies = TRUE)
    
    library(ggplot2)
    library(mapdata)
    
    mp1 <- fortify(map(fill=TRUE, plot=FALSE))
    mp2 <- mp1
    mp2$long <- mp2$long + 360
    mp2$group <- mp2$group + max(mp2$group) + 1
    mp <- rbind(mp1, mp2)
    ggplot(aes(x = long, y = lat, group = group), data = mp) + 
      geom_path()  + 
      scale_x_continuous(limits = c(110, 300)) + 
      scale_y_continuous(limits = c(-50, 70)) 
    

    【讨论】:

    • 感谢您的建议。我更新了我的问题,指定需要正确的经度值。
    【解决方案2】:

    类似:

    library(sp)
    library(maps)
    library(maptools)
    library(ggplot2)
    library(ggthemes)
    
    world <- map("world", fill=TRUE, col="transparent", plot=FALSE)
    worldSpP <- map2SpatialPolygons(world, world$names, CRS("+proj=longlat +ellps=WGS84"))
    worldSpP <- worldSpP[-grep("Antarctica", row.names(worldSpP)),]
    worldSpP <- worldSpP[-grep("Ghana", row.names(worldSpP)),]
    worldSpP <- worldSpP[-grep("UK:Great Britain", row.names(worldSpP)),]
    worldSpPnr <- nowrapRecenter(worldSpP)
    
    world_map <- fortify(worldSpPnr)
    
    gg <- ggplot()
    gg <- gg + geom_map(data=world_map, map=world_map,
                        aes(x=long, y=lat, map_id=id),
                        color="black", fill="white", size=0.25)
    gg <- gg + coord_map()
    gg <- gg + theme_map()
    gg
    

    【讨论】:

    • 感谢您的建议。我更新了我的问题,指定需要正确的经度值。
    猜你喜欢
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 2017-01-29
    相关资源
    最近更新 更多