在ggplot中绘制geom_polygon(来自gcIntermediate)时获取半圆

时间:2017-10-27 16:26:37

标签: r ggplot2

我尝试使用gcIntermediate函数生成的数据在地图上绘制曲线。

生成的数据如下:

smpl <- dput(structure(list(lon = c(13.2344270413, 8.8577600203052, 4.51528290435032, 
0.198299650985246, -4.10226301409485, -8.39571150433676, -12.6914508665082, 
-16.9988545420516, -21.3271327470757, -25.6851939271081, -30.0814942935843, 
-34.523871507901, -39.0193602432216, -43.5739897307105, -48.1925665583827, 
-52.8784499164751, -57.6333309856953, -62.4570328184276, -67.34735116188, 
-72.299959254216, -77.3083996028486, -82.3641821713), lat = c(-8.83828611363, 
-7.0372988947845, -5.19599594783464, -3.32519713389036, -1.43564398758235, 
0.461976834450922, 2.3570034948603, 4.23876609617668, 6.09655470681773, 
7.91959203957164, 9.69701462841945, 11.4178664927729, 13.0711094040183, 
14.6456538863455, 16.1304148728656, 17.5143953590842, 18.7868003057364, 
19.9371813254414, 20.9556102961325, 21.8328770526584, 22.5607029551946, 
23.1319588409), i = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4)), .Names = c("lon", "lat", "i"), row.names = 67:88, class = "data.frame"))

我的代码是:

x <- c('tidyverse', 'foreign', 'ggplot2', 'geosphere', 'maps')
sapply(X = x, FUN = require, character.only = TRUE)

wd = map_data("world")

ggplot() +
  geom_polygon(data = wd, aes(x = long, y = lat, group = group), fill = '#1c2850', colour = '#1c2850', alpha = 1) +
  geom_polygon(data = smpl, aes(x = lon, y = lat, group = i), colour = 'red')

我期待一条曲线,但我得到了一个半圆。用图片可以更好地说明这一点:

My map

我只想要一条相当卷曲的线 - 为什么我有一个丑陋的半圆?

1 个答案:

答案 0 :(得分:0)

感谢TylerRinker在评论中给出答案。

答案是使用geom_path,这会给出一条线而不是形状(geom_polygon按定义返回一个形状)

更新的代码是:

ggplot() +
  geom_polygon(data = wd, aes(x = long, y = lat, group = group), fill = '#1c2850', colour = '#1c2850', alpha = 1) +
  geom_path(data = smpl, aes(x = lon, y = lat, group = i), colour = 'red')