如何使用radial.plot/ggplot2创建显示每月存在或不存在的圆形图 [英] How to create a circular plot showing monthly presence or absence using radial.plot / ggplot2
问题描述
我想在ggplot2
中创建一个数字(外观类似于附件),该数字是圆形的,以一年中的月份为轴,因此在圆周上有十二个刻度线,其中一个内圆弧的某种颜色对应于月份,其中pa
= 1(其他选项是pa
= 0),另一个与sampled
对应的内部弧(在第一弧的外部)= 1(其他选项是sampled
= 0).数据如下:
I want to create a figure (similar looking to the attached) in ggplot2
that is circular with month of year represented on axis so twelve tick marks around the circumference, with one inner arc of a certain color corresponding to months where pa
= 1 (the other options is pa
= 0) and another inner arc (outside of the first arc) corresponding to sampled
= 1 (other option is sampled
= 0). Data looks like this:
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()
一起使用,但这只是一个饼图.
I was previously using ggplot(m, aes(season, fill=season))
with geom_bar(width=1)
and coord_polar()
but that just gives me a pie chart.
现在尝试:
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尚未被调用
plot.new has not been called yet
我认为我误解了radial.plot
所需的输入,并可能为所需的输出使用了错误的功能.
I think I'm misunderstanding what input radial.plot
needs and potentially using the wrong function for my desired output.
推荐答案
我不确定如何将数据与上面的图相关联,但是下面的代码有帮助吗?
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()
这篇关于如何使用radial.plot/ggplot2创建显示每月存在或不存在的圆形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!