显示方位角和距离的圆形图

时间:2019-01-10 16:19:30

标签: r ggplot2 plot

我试图显示个人离开某个地点的方位以及一天之内的距离,以及在不同方位上行驶的人数。

目前,我可以在ggplot2中进行这些基本绘制:

# distance & bearing
ggplot(data=df,
       aes(bear, dist)) +
  geom_point() +
  scale_y_log10()

# no. individuals & bearing
ggplot(data=df.postDepDT,
       aes(bear)) +
  geom_histogram(bins = 36)

#my data looks like the following
bear <- c(-172, -175, -160, -155, -150, -10, 23, 87, 122, 179)
dist <- c(5, 101, 326, 47, 23, 55, 6, 7, 44, 162)
df <- data.frame(bear, dist)

我希望我的最终情节有两个小组。第一个是一个圆,其点的不同长度(指示距离)沿轴承方向从圆向外辐射。这些点将沿着轴承连接到圆。第二个是围绕圆的直方图,显示了沿着每个方位角离开圆的人数。

在这些图中,0o(或北)在顶部,为-180o / 180o(南)在底部,左右分别为-90o和90o(西和东)。

更新:

在图上添加coord_polar()有助于制作圆形图,但是,我仍然无法在图的顶部获得0o值(对于North)(start = 0会将0o值置于底部) 。另外,我仍在寻找一种解决方案,以将第一个图中的点连接到中心点。

2 个答案:

答案 0 :(得分:2)

您是否正在寻找类似的东西?

illustration

var cars1 = [
    {id: 2, make: "Honda", model: "Civic", year: 2001},
    {id: 1, make: "Ford",  model: "F150",  year: 2002},
    {id: 3, make: "Chevy", model: "Tahoe", year: 2003},
];

var cars2 = [
    {id: 3, make: "Kia",    model: "Optima",  year: 2001},
    {id: 4, make: "Nissan", model: "Sentra",  year: 1982},
    {id: 2, make: "Toyota", model: "Corolla", year: 1980},
];
const cars1IDs = new Set(cars1.map(({ id }) => id));
const combined = [
  ...cars1,
  ...cars2.filter(({ id }) => !cars1IDs.has(id))
];
combined.sort(({ id: aId }, {id: bId }) => aId - bId);
console.log(combined);

说明

  1. ggplot(data=df, aes(bear, dist)) + geom_segment(aes(xend = bear, yend = 0.1)) + geom_point() + scale_x_continuous(limits = c(-180, 180), breaks = seq(-180, 180, 90)) + # scale_y_log10() + coord_polar(start = pi) + theme_bw() 期望geom_segment()xyxend的美观。圆形图表的“前{yend”版本具有从中心放射出的线,应该是直角坐标图中的图表,其线垂直向下垂落以与x轴相交,因此我们希望每个{{ 1}}的值应与coord_polar的值相同,并且每个xend的值应尽可能小(距离为0,但由于您对y的对数刻度感兴趣轴,我添加了一个小的正值):

cartesian form

x
  1. yend的{​​{1}}参数(添加了强调)期望如下:
  

从12点开始以弧度

为起点的偏移

由于轴承的实际值在ggplot(data=df, aes(bear, dist)) + geom_segment(aes(xend = bear, yend = 0.1)) + geom_point() + scale_x_continuous(limits = c(-180, 180), breaks = seq(-180, 180, 90)) + # scale_y_log10() + # coord_polar(start = pi) + theme_bw() 范围内,因此我希望您的比例尺为coord_polar()。默认的start会将c(-175, 179)放在12点钟的位置。要将c(-180, 180)放置在12点钟位置,请设置start = 0,弧度为180度。

  1. 最后,我敦促您仔细考虑为什么,您想使用对数刻度来表示距离。从技术上讲,为-180/180选择一个较小的正值可能会导致同一点看上去离中心的距离不同。 (有关一些0值,请参见下面的插图)从理论上讲,我理解对数刻度在比较大范围的数量时是合适的,在这里实际上不是这种情况。

illustrations with different yend values

start = pi

答案 1 :(得分:-2)

我相信这就是您要寻找的

add geom_bar to circular plot ggplot2