How to create a circular plot showing monthly presence or absence using radial.plot / ggplot2

爷,独闯天下 提交于 2019-12-04 07:51:22

I wasn't sure how to relate your data to the plot above, but does the following code help?

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()

Here are two different approaches in ggplot2 to convert the supplied data into a polar plot. The two approaches differ in the way month is treated.

Read data

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")

Prepare data

# 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"))]

Variant 1: months as factor

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)

Variant 2: months as period (with start and end date)

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)

Variant 3: coloured background

In response to a request in the comments, this variant colours the background for each variable individually while the ring segments are kept in black colour. So, if we would add a third variable it would get its own coloured background ring.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!