如何在 R 中的极坐标中添加时间维度?
How can I add time dimension in polar coordinates in R?
我正在尝试用 ggplot2
绘制南极周围一只鸟的轨迹。到目前为止,我得到了一张以极坐标投影的地图,我还设法正确绘制了轨迹点,我几乎 link 它们正确但是......当轨迹穿过国际 DATE & TIME 线 , ggplot2
无法正确 link line.So 两侧的 2 个点 我正在寻找一种方法来强制 ggplot link连续点数。
这是我的数据集:
Data =>
ID Date Time A1 Lat. Long.
10 12.9.2008 22:00 1 21.14092 70.98817
10 12.9.2008 22:20 1 21.13031 70.97592
10 12.9.2008 22:40 2 21.13522 70.97853
10 12.9.2008 23:00 1 21.13731 70.97817
10 12.9.2008 23:20 3 21.14197 70.97981
10 12.9.2008 23:40 1 21.14156 70.98158
10 12.9.2008 23:40 1 21.14156 70.98158
10 13.9.2008 00:00 2 21.14150 70.98478
10 13.9.2008 00:20 3 21.14117 70.98803
10 13.9.2008 00:40 1 21.14117 70.98803
10 13.9.2008 01:00 2 21.14117 70.98803
....
ID 是小鸟的 ID。
使用 Nick K 代码进行更新
这是我没有使用时间维度和线的原始图表
south_map <- map_data("world") %>% group_by(group)
set.seed(123)
track_df2 <- new_df2
long_diff <- diff(new_df2$Long)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
long_diff[long_diff > 180] <- long_diff[long_diff > 180] - 360
track_df2$Longitude <- cumsum(c(new_df2$Long[1], long_diff))
ggplot(track_df2, aes(x = track_df2$Long, y = track_df2$Lat)) +
geom_polygon(aes(group = a3_id), data = south_map, colour = "grey", fill = "gainsboro") +
geom_point(aes(colour = factor(a3_id)), size = 2)
A1 定义了鸟当前正在做什么。
你似乎并没有在你的情节中实际使用时间,但问题是经度环绕 -180/180。这可以使用 coord_map
而不是 coord_polar
并确保经度不会环绕来解决。
加载包并生成示例数据
library("ggplot2")
library("dplyr")
south_map <- map_data("world") %>% group_by(group) %>% filter(min(lat) <= -20)
set.seed(123)
track <- data.frame(long = cumsum(c(210,
unlist(lapply(c(1, -1), function(x) {
rnorm(50, x * 4, 4)
})))) %% 360 - 180,
lat = cumsum(c(-50, rnorm(100, 0.4, 2))),
A1 = sample(1:3, 101, replace = TRUE))
确保坐标不环绕:
track_new <- track
long_diff <- diff(track$long)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
long_diff[long_diff > 180] <- long_diff[long_diff > 180] - 360
track_new$long <- cumsum(c(track$long[1], long_diff))
使用等距投影绘图。请注意,这假定北极位于中心,因此纬度被翻转,然后使用比例尺进行校正。
ggplot(track_new, aes(x = long, y = -lat)) +
geom_polygon(aes(group = group), data = south_map, colour = "grey", fill = "gainsboro") +
coord_map("azequidistant") +
geom_point(aes(colour = factor(A1)), size = 2) +
geom_path(colour = "grey", size = 1) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous("latitude", breaks = 25 * 0:3, labels = -25 * 0:3)
最终剧情:
只是出于兴趣,我认为制作这张图片的动画会很有趣。这是执行此操作的代码:
track_new$alpha <- 1
# Setup longitude labels
long_labels <- data.frame(long = 45 * -3:4, lat = -22.5)
long_labels$label <- long_labels$long
long_labels$label[8] <- "\U00B1 180"
long_labels$angle <- long_labels$long + 67.5 + 180 * (long_labels$long >= 45)
# Set up the basic plot
p <- ggplot(track_new, aes(x = long, y = -lat)) +
geom_polygon(aes(group = group), data = south_map, colour = "grey", fill = "gainsboro") +
coord_map("azequidistant", ylim = c(20, 90)) +
geom_point(aes(colour = A1, alpha = alpha), size = 2) +
geom_path(aes(alpha = alpha), colour = "grey", size = 1) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous("latitude", breaks = 22.5 * 0:3, labels = -22.5 * 0:3) +
scale_alpha_identity(guide = "none") +
geom_text(aes(label = label, angle = angle),
data = long_labels, colour = "dark blue", alpha = 0.5, size = 4)
# Produce the animation
p$data$alpha <- 0
for(i in 1:(nrow(track_new) + 10)) {
p$data$alpha <- pmax(p$data$alpha - 0.1, 0)
if (i <= nrow(track_new)) {
p$data$alpha[i] <- 1
}
png(file.path("BirdPlots", sprintf("BirdPlot%03d.png", i)), width = 1024, height = 1024, res = 100)
print(p)
dev.off()
if (!(i %% 5)) cat(i, "\n")
}
# This needs ImageMagick in the system path. For non-Windows systems, you
# might be better using system rather than shell
shell(paste("convert", file.path("BirdPlots", "BirdPlot*.png"),
file.path("BirdPlots", "BirdPlotAnimation.gif")))
结果如下:
编辑 ayush 代码的更正版本
track_df2 <- new_df2
long_diff <- diff(new_df2$Longitude)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
long_diff[long_diff > 180] <- long_diff[long_diff > 180] - 360
track_df2$Longitude <- cumsum(c(new_df2$Longitude[1], long_diff))
track_df2$a3_id <- factor(track_df2$a3_id)
ggplot(track_df2, aes(x = Longitude, y = -Latitude)) +
coord_map("azequidistant", ylim = c(20, 90)) +
geom_point(aes(colour = a3_id, alpha = alpha), size = 2) +
geom_path(aes(alpha = alpha), colour = "grey", size = 1) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = 22.5 * 0:3, labels = -22.5 * 0:3) +
scale_alpha_identity(guide = "none")
我正在尝试用 ggplot2
绘制南极周围一只鸟的轨迹。到目前为止,我得到了一张以极坐标投影的地图,我还设法正确绘制了轨迹点,我几乎 link 它们正确但是......当轨迹穿过国际 DATE & TIME 线 , ggplot2
无法正确 link line.So 两侧的 2 个点 我正在寻找一种方法来强制 ggplot link连续点数。
这是我的数据集:
Data =>
ID Date Time A1 Lat. Long.
10 12.9.2008 22:00 1 21.14092 70.98817
10 12.9.2008 22:20 1 21.13031 70.97592
10 12.9.2008 22:40 2 21.13522 70.97853
10 12.9.2008 23:00 1 21.13731 70.97817
10 12.9.2008 23:20 3 21.14197 70.97981
10 12.9.2008 23:40 1 21.14156 70.98158
10 12.9.2008 23:40 1 21.14156 70.98158
10 13.9.2008 00:00 2 21.14150 70.98478
10 13.9.2008 00:20 3 21.14117 70.98803
10 13.9.2008 00:40 1 21.14117 70.98803
10 13.9.2008 01:00 2 21.14117 70.98803
....
ID 是小鸟的 ID。
使用 Nick K 代码进行更新
这是我没有使用时间维度和线的原始图表
south_map <- map_data("world") %>% group_by(group)
set.seed(123)
track_df2 <- new_df2
long_diff <- diff(new_df2$Long)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
long_diff[long_diff > 180] <- long_diff[long_diff > 180] - 360
track_df2$Longitude <- cumsum(c(new_df2$Long[1], long_diff))
ggplot(track_df2, aes(x = track_df2$Long, y = track_df2$Lat)) +
geom_polygon(aes(group = a3_id), data = south_map, colour = "grey", fill = "gainsboro") +
geom_point(aes(colour = factor(a3_id)), size = 2)
A1 定义了鸟当前正在做什么。
你似乎并没有在你的情节中实际使用时间,但问题是经度环绕 -180/180。这可以使用 coord_map
而不是 coord_polar
并确保经度不会环绕来解决。
加载包并生成示例数据
library("ggplot2")
library("dplyr")
south_map <- map_data("world") %>% group_by(group) %>% filter(min(lat) <= -20)
set.seed(123)
track <- data.frame(long = cumsum(c(210,
unlist(lapply(c(1, -1), function(x) {
rnorm(50, x * 4, 4)
})))) %% 360 - 180,
lat = cumsum(c(-50, rnorm(100, 0.4, 2))),
A1 = sample(1:3, 101, replace = TRUE))
确保坐标不环绕:
track_new <- track
long_diff <- diff(track$long)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
long_diff[long_diff > 180] <- long_diff[long_diff > 180] - 360
track_new$long <- cumsum(c(track$long[1], long_diff))
使用等距投影绘图。请注意,这假定北极位于中心,因此纬度被翻转,然后使用比例尺进行校正。
ggplot(track_new, aes(x = long, y = -lat)) +
geom_polygon(aes(group = group), data = south_map, colour = "grey", fill = "gainsboro") +
coord_map("azequidistant") +
geom_point(aes(colour = factor(A1)), size = 2) +
geom_path(colour = "grey", size = 1) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous("latitude", breaks = 25 * 0:3, labels = -25 * 0:3)
最终剧情:
只是出于兴趣,我认为制作这张图片的动画会很有趣。这是执行此操作的代码:
track_new$alpha <- 1
# Setup longitude labels
long_labels <- data.frame(long = 45 * -3:4, lat = -22.5)
long_labels$label <- long_labels$long
long_labels$label[8] <- "\U00B1 180"
long_labels$angle <- long_labels$long + 67.5 + 180 * (long_labels$long >= 45)
# Set up the basic plot
p <- ggplot(track_new, aes(x = long, y = -lat)) +
geom_polygon(aes(group = group), data = south_map, colour = "grey", fill = "gainsboro") +
coord_map("azequidistant", ylim = c(20, 90)) +
geom_point(aes(colour = A1, alpha = alpha), size = 2) +
geom_path(aes(alpha = alpha), colour = "grey", size = 1) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous("latitude", breaks = 22.5 * 0:3, labels = -22.5 * 0:3) +
scale_alpha_identity(guide = "none") +
geom_text(aes(label = label, angle = angle),
data = long_labels, colour = "dark blue", alpha = 0.5, size = 4)
# Produce the animation
p$data$alpha <- 0
for(i in 1:(nrow(track_new) + 10)) {
p$data$alpha <- pmax(p$data$alpha - 0.1, 0)
if (i <= nrow(track_new)) {
p$data$alpha[i] <- 1
}
png(file.path("BirdPlots", sprintf("BirdPlot%03d.png", i)), width = 1024, height = 1024, res = 100)
print(p)
dev.off()
if (!(i %% 5)) cat(i, "\n")
}
# This needs ImageMagick in the system path. For non-Windows systems, you
# might be better using system rather than shell
shell(paste("convert", file.path("BirdPlots", "BirdPlot*.png"),
file.path("BirdPlots", "BirdPlotAnimation.gif")))
结果如下:
编辑 ayush 代码的更正版本
track_df2 <- new_df2
long_diff <- diff(new_df2$Longitude)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
long_diff[long_diff > 180] <- long_diff[long_diff > 180] - 360
track_df2$Longitude <- cumsum(c(new_df2$Longitude[1], long_diff))
track_df2$a3_id <- factor(track_df2$a3_id)
ggplot(track_df2, aes(x = Longitude, y = -Latitude)) +
coord_map("azequidistant", ylim = c(20, 90)) +
geom_point(aes(colour = a3_id, alpha = alpha), size = 2) +
geom_path(aes(alpha = alpha), colour = "grey", size = 1) +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = 22.5 * 0:3, labels = -22.5 * 0:3) +
scale_alpha_identity(guide = "none")