Search code examples
rggplot2plotrixradial

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


enter image description here

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

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.

Now trying:

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)

and getting the error

plot.new has not been called yet

I think I'm misunderstanding what input radial.plot needs and potentially using the wrong function for my desired output.


Solution

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

    enter image description here