如何使用radial.plot / ggplot2创建一个展示每月出现或缺席情况的圆形图。

6

这里输入图片描述

我想在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函数需要的输入,可能正在使用错误的函数来得到我想要的输出。


1
你能否发布一个你想要生成的图形示例(即使是在Paint或Gimp中快速手动完成)? - zoneparser
@zoneparser 我附上了一张照片...有什么想法吗?谢谢! - user5063841
看起来你需要使用geom_rect,因为你要绘制的所有对象都是矩形(在极坐标系下)。 - Mist
2个回答

9

以下是两种不同的 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)

变体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)

变体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


是否可以添加一个圆形的彩色背景,就像panel.background = element_rect(fill='grey'),然后再加上shape = 1(我知道你不能在element rect中添加形状,但也许有一种方法可以实现这个?) - user5063841
您可以尝试使用适当选择的宽度、高度和颜色作为第一层来绘制geom_rect(),以创建屏幕截图中形成背景的白色、浅蓝色和蓝色环。 - Uwe
1
我已经按要求添加了第三个变体,带有彩色背景环。 - Uwe

3

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接