• R语言已解决
  • 已解决:如何消除 ggplot 绘制局部世界地图时出现的冗余横线和色块?

dapengde 更改标题为「虽解决,不完美:如何消除 ggplot 绘制局部世界地图时出现的冗余横线和色块?

dapengde 用这个 geom_path()

require(ggplot2)
p <- ggplot(map_data("world"), aes(long,lat,group=group)) + geom_path()
p

    Cloud2016 geom_path() 是不行的,在 xlim = c(-140, 120)会出现冗余横线。

    geom_path()geom_polygon() 病根儿应该是相同的,只是临床症状有区别:前者出现多余的线,后者出现多余的面。

      Cloud2016 如果不考虑用 ggplot,那就好办了。例如用 maps 包就行:

      maps::map("world", fill=TRUE, col="white", bg="lightblue", xlim = c(-140, 120))

      这么说来,解决 ggplot 地图问题的方案就是不使用 ggplot 咯……

        Liechi 这又回到了 Base R 定义的问题了: maps 包算是 Base R 吗……

        为了避免无谓的口水战,最后可能会沦为 Tidyverse vs. Non-Tidyverse。所以可能改名叫 TNT 擂台赛。

          完美解决方案来啦:用 coord_quickmap():

          p + coord_quickmap(xlim = lr)

          同时满足了我的三个要求:无冗余色块,经纬度均匀,展示随意指定经纬度范围。

            dapengde 更改标题为「已解决:如何消除 ggplot 绘制局部世界地图时出现的冗余横线和色块?

            dapengde 顺便补充一下二者区别,来自帮助文档 ?coord_quickmap

            coord_map projects a portion of the earth, which is approximately spherical, onto a flat 2D plane using any projection defined by the mapproj package. Map projections do not, in general, preserve straight lines, so this requires considerable computation. coord_quickmap is a quick approximation that does preserve straight lines. It works best for smaller areas closer to the equator.

            4 年 后