将动物运动可视化为 R 中的图表
Visualize animal movement as chart in R
我有一个 table 有这样的动物运动数据:
5N
40 西北
10 周刊
10 瓦
15 牛
这背后的信息是:20只动物向北移动,40只向西北移动,10只向西南移动,10只向西移动。
我想生成一个类似 THIS 的图表来可视化走势。
我如何使用 R 来做到这一点?
您可能会发现软件包 fmsb 的 radarchart
功能很有帮助。
# example data
df <- data.frame(freq=c(5, 40, 10, 10, 15),
dir=c("N", "NW", "SW", "W", "N"))
# summarize by direction
dftot <- aggregate(freq ~ dir, df, sum)
# list of unique directions, starting at top and winding counterclockwise
uniqdir <- c("N", "NW", "W", "SW", "S", "SE", "E", "NE")
# arrange total counts in a matrix
df <- as.data.frame(matrix(dftot$freq[match(uniqdir, dftot$dir)], nrow=1,
dimnames=list(NULL, uniqdir)))
# replace missing values with zero
df[is.na(df)] <- 0
# add rows for maximum and minimums
df2 <- rbind(rep(max(df), length(uniqdir)), rep(0, length(uniqdir)), df)
# draw the plot
library(fmsb)
radarchart(df2, centerzero=TRUE)
我有一个 table 有这样的动物运动数据:
5N
40 西北
10 周刊
10 瓦
15 牛
这背后的信息是:20只动物向北移动,40只向西北移动,10只向西南移动,10只向西移动。
我想生成一个类似 THIS 的图表来可视化走势。
我如何使用 R 来做到这一点?
您可能会发现软件包 fmsb 的 radarchart
功能很有帮助。
# example data
df <- data.frame(freq=c(5, 40, 10, 10, 15),
dir=c("N", "NW", "SW", "W", "N"))
# summarize by direction
dftot <- aggregate(freq ~ dir, df, sum)
# list of unique directions, starting at top and winding counterclockwise
uniqdir <- c("N", "NW", "W", "SW", "S", "SE", "E", "NE")
# arrange total counts in a matrix
df <- as.data.frame(matrix(dftot$freq[match(uniqdir, dftot$dir)], nrow=1,
dimnames=list(NULL, uniqdir)))
# replace missing values with zero
df[is.na(df)] <- 0
# add rows for maximum and minimums
df2 <- rbind(rep(max(df), length(uniqdir)), rep(0, length(uniqdir)), df)
# draw the plot
library(fmsb)
radarchart(df2, centerzero=TRUE)