绘制ggplot2中两个位置之间的曲线 [英] Plot curved lines between two locations in ggplot2

查看:351
本文介绍了绘制ggplot2中两个位置之间的曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  data = data.frame(Country.name= c(Brazil,USA,Canada, 巴拉圭,乌拉圭),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)
区域。 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-1ng))),
箭头=箭头(长度=单位(0.5,厘米),角度= 45,类型=关闭))

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

由于某些原因,我无法运行 cshapes ,但这里有一个如何使用 curveGrob() code> grid 包和 ggplot2 annotation_custom() code>函数。它给你很大的灵活性。 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, (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至10,10
annotation_custom(grob = myCurve2,2.5,6,2.5,6)#plot from 2.5,2.5 to 6,6

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


I am making a map about exports from the Americas to the Netherlands. To visualise my data, I want to make a map with arrows from countries in the Americas to the Netherlands. I use a cshapes world map and 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"))

I have found that the lines overlap when they are plotted straight. This is ugly. Therefore, I am looking for a way to plot curved lines between coordinates within ggplot2, so they don't overlap.

解决方案

I couldn't run cshapes for some reason, but here's an example of how to build curves using curveGrob() from the grid package and ggplot2's annotation_custom() function. It gives you a lot of flexibility. PS: most of the params are just defaults. Edit - updated to show 2 curves.

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

这篇关于绘制ggplot2中两个位置之间的曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆