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

时间:2016-12-10 23:22:35

标签: r ggplot2 plotrix radial

enter image description here

我想在str.length中创建一个圆形图形(类似于附图),圆形表示轴上有一个月份,因此圆周周围有十二个刻度标记,其中一个内部弧形对应于某个颜色ggplot2 = 1(其他选项为pa = 0)的月份和与pa = 1对应的另一个内弧(第一个弧外)(其他选项为{{1} } = 0)。数据如下所示:

sampled

我以前使用sampled 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)

并收到错误

  

plot.new还没有被调用

我认为我误解了输入coord_polar()需要什么,并且可能使用错误的函数来获得我想要的输出。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

我不确定如何将您的数据与上图相关联,但以下代码是否有帮助?

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

enter image description here