geom_path()拒绝越过coord_polar()中的0/360行 [英] geom_path() refuses to cross over the 0/360 line in coord_polar()
问题描述
我正在尝试绘制随时间变化的物体(例如风向标)的角度。我想将其绘制在极坐标系上,并用一条路径连接时间点,以显示角度如何随时间变化。我只是有一个数据框,一列是角度(以度为单位)(数字),然后是记录角度的时间步长(整数)。
I'm trying to plot the angle of an object (let's say it's a weather vane) over time. I want to plot it on a polar coordinate system and have the time points be connected by a path, showing how the angle evolves over time. I simply have a dataframe, with one column being the angle in degrees (numeric) and then the time step when the angle was recorded (integer).
但是当我运行时下面的代码:
But when I run the below code:
ggplot(df, aes(x = angle.from.ref, y = time.step)) +
coord_polar() +
geom_path() +
geom_point() +
scale_x_continuous(limits = c(0, 360), breaks = seq(0, 360, 45))
我得到的东西看起来像这样:
I get something that looks like this:
geom_path()
拒绝越过0/360度线。如果359值后跟1值,则该路径不会创建穿过x = 0/360点的短链接。相反,路径沿圆弧一直向后弯曲,从另一侧到达x = 1。
The path created by geom_path()
refuses to cross the 0/360 degree line. If a value of 359 is followed by a value of 1, the path will not create a short link passing across the x=0/360 point. Instead, the path curves back ALL the way around the circle, arriving at x=1 from the other side.
我希望使用 coord_polar( )
可以解决此问题,但显然不能解决。有什么方法可以告诉 ggplot
值0和360是相邻/连续的吗?
I had hoped using coord_polar()
would have solved this, but clearly not. Is there some way I can tell ggplot
that the values 0 and 360 are adjacent/contiguous?
推荐答案
绕过交叉问题可能更直接:在360/0点进行插值,并将每转绘制为自己的部分。它的工作方式如下:
It may be more straightforward to bypass the crossing-over problem: interpolate at the 360/0 point, and plot each revolution as its own section. Here's how it can work:
library(dplyr)
library(ggplot2)
# sample data
n <- 100
df <- data.frame(
angle.from.ref = seq(0, 800, length.out = n),
time.step = seq(Sys.time(), by = "min", length.out = n)
)
df %>%
interpolate.revolutions() %>%
ggplot(aes(x = angle.from.ref, y = time.step,
group = revolution)) +
geom_line(aes(color = factor(revolution)), size = 1) + # color added for illustration
scale_x_continuous(limits = c(0, 360),
breaks = seq(0, 360, 45)) +
coord_polar()
interpolate.revolutions
函数的代码:
interpolate.revolutions <- function(df, threshold = 360){
# where df is a data frame with angle in the first column & radius in the second
res <- df
# add a label variable such that each span of 360 degrees belongs to
# a different revolution
res$revolution <- res[[1]] %/% threshold
# keep only the angle values within [0, 360 degrees]
res[[1]] <- res[[1]] %% threshold
# if there are multiple revolutions (i.e. the path needs to cross the 360/0 threshold),
# calculate interpolated values & add them to the data frame
if(n_distinct(res$revolution) > 1){
split.res <- split(res, res$revolution)
res <- split.res[[1]]
for(i in seq_along(split.res)[-1]){
interp.res <- rbind(res[res[[2]] == max(res[[2]]), ],
split.res[[i]][split.res[[i]][[2]] == min(split.res[[i]][[2]]), ])
interp.res[[2]] <- interp.res[[2]][[1]] +
(threshold - interp.res[[1]][1]) /
(threshold - interp.res[[1]][1] + interp.res[[1]][2]) *
diff(interp.res[[2]])
interp.res[[1]] <- c(threshold, 0)
res <- rbind(res, interp.res, split.res[[i]])
}
}
return(res)
}
此方法也可以应用于绘图中的多条线。只需将函数分别应用于每行:
This approach can be applied to multiple lines in a plot as well. Just apply the function separately to each line:
# sample data for two lines, for different angle values taken at different time points
df2 <- data.frame(
angle.from.ref = c(seq(0, 800, length.out = 0.75 * n),
seq(0, 1500, length.out = 0.25 * n)),
time.step = c(seq(Sys.time(), by = "min", length.out = 0.75 * n),
seq(Sys.time(), by = "min", length.out = 0.25 * n)),
line = c(rep(1, 0.75*n), rep(2, 0.25*n))
)
df2 %>%
tidyr::nest(-line) %>%
mutate(data = purrr::map(data, interpolate.revolutions)) %>%
tidyr::unnest() %>%
ggplot(aes(x = angle.from.ref, y = time.step,
group = interaction(line, revolution),
color = factor(line))) +
geom_line(size = 1) +
scale_x_continuous(limits = c(0, 360),
breaks = seq(0, 360, 45)) +
coord_polar()
这篇关于geom_path()拒绝越过coord_polar()中的0/360行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!