gpt4 book ai didi

r - 如何使用radial.plot/ggplot2创建显示每月存在或不存在的圆形图

转载 作者:行者123 更新时间:2023-12-04 11:07:25 25 4
gpt4 key购买 nike

enter image description here

我想在 ggplot2 中创建一个图形(类似于附件)这是一个圆形,在轴上表示一年中的月份,因此圆周上有十二个刻度线,一个特定颜色的内弧对应于月份,其中pa = 1(其他选项为 pa = 0)和另一个内弧(第一个弧外)对应于 sampled = 1(另一个选项是 sampled = 0)。数据如下所示:

 m
season pa month sampled occupancy
1 spring 1 3 1 present
2 spring 1 4 1 present
3 spring 1 5 1 present
4 summer 1 6 1 present
5 summer 1 7 1 present
6 summer 1 8 1 present
7 winter 0 12 1 absent
8 winter 0 1 0 absent
9 winter 0 2 0 absent
10 fall 1 9 1 present
11 fall 1 10 1 present
12 fall 1 11 1 present

我以前用过 ggplot(m, aes(season, fill=season))geom_bar(width=1)coord_polar()但这只是给了我一个饼图。

现在尝试:
radial.plot(m, radial.pos=c(1,2,3,4,5,6,7,8,9,10,11,12), labels="", rp.type="r",
line.col=8, lwd=3, add=TRUE)

并得到错误

plot.new has not been called yet



我想我误解了什么输入 radial.plot需要并可能对我想要的输出使用错误的函数。

最佳答案

以下是 ggplot2 中的两种不同方法将提供的数据转换为极坐标图。这两种方法的不同之处在于 month被治疗。

读取数据

library(data.table)
m <- fread("id season pa month sampled occupancy
1 spring 1 3 1 present
2 spring 1 4 1 present
3 spring 1 5 1 present
4 summer 1 6 1 present
5 summer 1 7 1 present
6 summer 1 8 1 present
7 winter 0 12 1 absent
8 winter 0 1 0 absent
9 winter 0 2 0 absent
10 fall 1 9 1 present
11 fall 1 10 1 present
12 fall 1 11 1 present")

准备数据
# reshape from wide to long (as preferred by ggplot)
ml <- melt(m, measure.vars = c("pa", "sampled"))
# create factors to ensure desired order
ml[, variable := factor(variable, levels = c("pa", "sampled"))]

变体 1:月份作为因素
ml[, fct_month := factor(month, levels = 1:12, labels = month.abb)]
library(ggplot2)
ggplot(ml[value != 0], aes(x = fct_month, y = variable,
group = variable, colour = variable)) +
geom_line(size = 5) +
scale_y_discrete(expand = c(0, 1), breaks = NULL) +
xlim(month.abb) +
coord_polar() +
theme_bw() + xlab(NULL) + ylab(NULL)

enter image description here

变体 2:月份作为期间(带有开始和结束日期)
ml[, start := as.Date("2016-01-01") + base::months(month - 1L)]
# last day of month = begin of next month - 1 day
ml[, end := as.Date("2016-01-01") + base::months(month) - 1L]
library(ggplot2)
ggplot(ml, aes(x = start, xend = end, y = variable, yend = variable,
group = variable, colour = variable,
size = value)) +
geom_segment() +
scale_y_discrete(expand = c(0, 1), breaks = NULL) +
scale_x_date(date_breaks = "1 month", date_labels = month.abb,
limits = range(c(ml$start, ml$end))) +
scale_size(guide = FALSE) +
coord_polar() +
theme_bw() + xlab(NULL) + ylab(NULL)

enter image description here

变体 3:彩色背景

为响应评论中的请求,该变体为每个变量单独着色背景,而环段保持为黑色。因此,如果我们添加第三个变量,它将获得自己的彩色背景环。
ml[, start_year := as.Date("2016-01-01")]
# last day of month = begin of next month - 1 day
ml[, end_year := as.Date("2016-12-31")]
ml[, start := start_year + base::months(month - 1L)]
ml[, end := start_year + base::months(month) - 1L]

library(ggplot2)
bg_height <- 1.0
ggplot(ml) +
geom_rect(aes(xmin = start_year, xmax = end_year,
ymin = as.integer(variable) - 0.5 * bg_height,
ymax = as.integer(variable) + 0.5 * bg_height,
group = variable, fill = variable)
) +
geom_segment(aes(x = start, xend = end, y = variable, yend = variable,
group = variable, size = value),
colour = "gray20") +
scale_y_discrete(expand = c(0, 1), breaks = NULL) +
scale_x_date(date_breaks = "1 month", date_labels = month.abb,
limits = range(c(ml$start, ml$end))) +
scale_size(guide = FALSE) +
scale_fill_brewer(palette = "Blues") +
coord_polar() +
theme_bw() + xlab(NULL) + ylab(NULL)

enter image description here

关于r - 如何使用radial.plot/ggplot2创建显示每月存在或不存在的圆形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41081376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com