用于绘制ggplot中的点范围的geom_segment的替代方案

时间:2017-03-09 00:45:24

标签: r ggplot2

我正在绘制VHF接收器位置的地图。对于每个接收器位置,我想添加连接到每个接收器的天线的估计检测范围(视距12km)。

我已经能够使用如下所示的geom_segment执行此操作:

enter image description here 但我想更准确地用气球而不是线段来表示天线检测范围(下面的示例图片)

Map of Receiver Locations with desired detection ranges

以下是我目前重现线段方法的代码:

   library(ggmap);library(ggplot2)

   tower <- data.frame(id="somewhere", lat = 29.5634, lon = -82.6111)

    map1 <- get_map(location = tower[,c("lon","lat")], maptype = "satellite", zoom=9)  

    tower$start = tower$lon - 0.2 # creates segment length of approx 12km in degrees
    tower$end = tower$lon + 0.2 # creates segment length of approx

    ggmap(map1) +  geom_segment(data=tower, aes(x=tower$start, 
                             y=tower$lat, 
                             xend=tower$end, 
                             yend=tower$lat), 
                             size=1, colour="red") + 
                    geom_point(data=tower, aes(x=lon, y=lat), colour="black") 

有关如何重新创建示例图的任何建议都将不胜感激。

由于 科比

1 个答案:

答案 0 :(得分:2)

它有点冗长,但我们可以添加四个geom_curve来创建该形状。

注意最后的coord_cartesian()geom_curve is not implemented for non-linear coordinates所以我们强迫coord为笛卡尔坐标。这意味着此方法仅适用于小规模。

ggmap(map1) + 
  # geom_segment(data = tower,
  #              aes(x = start,
  #                  y = lat,
  #                  xend = end,
  #                  yend = lat),
  #              size = 1, colour = "red") +
  geom_curve(data = tower, 
             aes(x = lon, 
                 y = lat, 
                 xend = I(lon - .2), 
             curvature = -.5,
             angle = 45,color = 'yellow') +
  geom_curve(data = tower, 
             aes(x = lon, 
                 y = lat, 
                 xend = end, 
                 yend = lat),
             curvature = .5,
             angle = 135,color = 'yellow') +
  geom_curve(data = tower,
             aes(x = lon,
                 y = lat,
                 xend = start,
                 yend = lat),
             curvature = .5,
             angle = 135,color = 'yellow') +
  geom_curve(data = tower,
             aes(x = lon,
                 y = lat,
                 xend = end,
                 yend = lat),
             curvature = -.5,
             angle = 45,color = 'yellow') +
  geom_point(data = tower, aes(x = lon, y = lat), colour = "black") +
  coord_cartesian()

enter image description here

我猜也可以创建一个新的geom_*