[英]How to create a circular plot showing monthly presence or absence using radial.plot / ggplot2
我想在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還沒有被調用
我想我誤解了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"))]
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)
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)
響應評論中的請求,此變體單獨為每個變量着色背景,而環段保持黑色。 所以,如果我們要添加第三個變量,它將獲得自己的彩色背景環。
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)
我不確定如何將您的數據與上圖相關聯,但以下代碼是否有幫助?
df <- data.frame(startdate = as.Date(c("2016-02-20", "2016-02-20", "2016-02-20")),
finishdate = as.Date(c("2016-04-30", "2016-04-30", "2016-06-10")),
y = c(4,5,8))
library(ggplot2)
ggplot(df) +
geom_rect(aes(xmin = startdate,
xmax = finishdate,
ymin = y-0.2,
ymax = y + 0.2)) +
geom_rect(aes(xmin = startdate - 5,
xmax = finishdate + 5,
ymin = y-0.05,
ymax = y + 0.05)) +
xlim(as.Date("2016-01-01"), as.Date("2016-12-31")) +
ylim(0,10) +
coord_polar()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.