【问题标题】:Plot curved lines between two locations in ggplot2在ggplot2中的两个位置之间绘制曲线
【发布时间】:2013-12-11 13:02:01
【问题描述】:

我正在制作一张关于从美洲出口到荷兰的地图。为了可视化我的数据,我想制作一张带有从美洲国家到荷兰的箭头的地图。我使用 cshapes 世界地图和 ggplot2。

data = data.frame("Country.name" = c("Brazil","USA","Canada","Paraguay","Uruguay"), "lng" =    
c(14.23,37,56.13,-23.44,-32.52), "lat" = c(-51.92,-95.71,-106.34,-58.44,-55.77))

require(cshapes)
cshp.data = cshp(date=as.Date("2012-1-1"), useGW=TRUE)
region.data.frame = fortify(cshp.data, region = "ISO1AL3")

ggplot(region.data.frame) + geom_polygon(aes(long,lat,group=group)) +
geom_segment(data = data, aes(x = lat, y = lng, xend= (5.29 - 0.1 * (5.29 - lat)), yend= (52.13 - 0.1 * (52.13 - lng))),
arrow=arrow(length=unit(0.5,"cm"), angle = 45, type = "closed"))

我发现直线绘制时会重叠。这很丑陋。因此,我正在寻找一种在 ggplot2 内的坐标之间绘制曲线的方法,因此它们不会重叠。

【问题讨论】:

标签: r plot ggplot2 geo


【解决方案1】:

由于某种原因,我无法运行 cshapes,但这里有一个示例,说明如何使用 grid 包中的 curveGrob()ggplot2annotation_custom() 函数构建曲线。它为您提供了很大的灵活性。 PS:大多数参数只是默认值。编辑 - 更新为显示 2 条曲线。

require(grid)
g<-qplot(c(0,10),c(0,10))
myCurve<-curveGrob(0, 0, 1, 1, default.units = "npc",
               curvature = 0.3, angle = 90, ncp = 20, shape = 1,
               square = FALSE, squareShape = 1,
               inflect = FALSE, arrow = arrow(), open = TRUE,
               debug = FALSE,
               name = NULL, gp = gpar(), vp = NULL)

myCurve2<-curveGrob(0, 0, 1, 1, default.units = "npc",
               curvature = -0.3, angle = 60, ncp = 10, shape = 1,
               square = FALSE, squareShape = 1,
               inflect = FALSE, arrow = arrow(), open = TRUE,
               debug = FALSE,
               name = NULL, gp = gpar(), vp = NULL)

g + 
  annotation_custom(grob=myCurve,0,10,0,10) + # plot from 0,0 to 10,10
  annotation_custom(grob=myCurve2,2.5,6,2.5,6)   # plot from 2.5,2.5 to 6,6

#REFERENCE>>http://stat.ethz.ch/R-manual/R-devel/library/grid/html/grid.curve.html

【讨论】:

    猜你喜欢
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2013-12-19
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多