TeddyTedTed
TeddyTedTed

Reputation: 103

How do I add divisions to a circular plot in R?

I am plotting the density of observations throughout the year and I want to add radial lines which delimit the months of the year.

My dataframe is called Data.1

Data.1$YearDayDeg is a column which has dates converted into degrees.

This is my code:

circ = as.circular(Data.1$YearDayDeg, units = "degrees")

circdens = density(circ, bw = 100, control.circular = list(units = "degrees"))

plot(circdens, tol = 0.2, zero = 1.57079633, rotation = "clock")

This produced this plot but I want to add radial lines to display months with labels and get rid of the stupid automatic title and add my own custom title so it ends up looking something like this.

Any help would be great, thanks.

Upvotes: 0

Views: 499

Answers (2)

Martin
Martin

Reputation: 594

Regarding the quenstions:

  • radial lines -> use segments display months -> use text
  • custom title -> parameter main="Your title" in function plot
  • get rid of labels showing 0, 90, 180 and 270 degrees -> use axes=FALSE
    plotting circular objects seems to work via plot.circular, which has - as usual - the axes parameter, where axes=FALSE suppresses axes (with labels).
  • start and rotational direction of labels: updated the maths. Always recommend to plot first with axis to check custom labels, and only then suppress axes ;-)

See also online help for details: simply run ?plot, ?par, ?plot, ?plot.circular, ...).
Hope that helps!

# always a good idea to state which packages are used...
require(circular)
require(stats)

# reproducible sampel data
set.seed(123)
dta <- runif(n=360,min=0,max=360)  

# get "plottable" data
circ = as.circular(dta, units = "degrees")
circdens = density(circ, bw = 100, control.circular = list(units = "degrees"))

# plot
par.xpd.old <- par("xpd") # default is FALSE
par(xpd=NA,  # clip plotting only at device region, not to plot region (default). See ?par
    mar=c(5, 4, 4, 2) + 0.1 + c(1,0,2,0))    # increase margin space for outward shifted month labels; default is c(5, 4, 4, 2) + 0.1

plot(circdens, zero=pi/2, tol = 0.2, 
     rotation = "clock",
     main=NA, #"Your Title",    # your title (displays template used)
     sub="your subtitle",       # and some other stuff to play around
     col="red", lty="solid",    # see ?plot and ?par
     # xlab="x axis label instead of N, Bandwidth and units",
     ylab="y axis label instead of Density circular",
     # template=c("none", "geographics", "clock12", "clock24")[1], #does not help much regarding the question
     # units="",  "" -> pi units
     axes=FALSE) # always check frist (axes=TRUE) if real labels and custom labels match... 

# if you want to move the "main title", try also plot(... main=NA...), and then:
mtext(text="Do it yourself title", side=3, line=4, cex=1.5, font=2)

# radial lines
segments(x0=0, y0=0, 
         x1=cos(seq(from=0, to=2*pi, length.out=12+1)) * 1.5,   # do some maths...
         y1=sin(seq(from=0, to=2*pi, length.out=12+1)) * 1.5,   # *1.5: extend radial lines outwards
         col="blue")

# mont labels
text(cos(seq(from=2*pi-pi/12, to=0-pi/12, length.out=12+1)+pi/2) * 1.5,  # offset -pi/12 places labels in between dividing lines
     sin(seq(from=2*pi-pi/12, to=0-pi/12, length.out=12+1)+pi/2) * 1.5,  # *1.5 places the months a little bit outwards
     labels=c("January", "February", "March", "April", "May", "June", 
              "July", "August", "September", "October", "November", "December", NA), # need 12+1 data
     col="blue")

par(xpd=par.xpd.old)  # restore previous xpd settings

# --- end ---

Upvotes: 3

Stedy
Stedy

Reputation: 7469

In addition to circular, there is another library called plotrix which can help achieve the desired result:

library(plotrix)
testlen <- c(rnorm(36)*2+5)
testpos <- seq(0,350,by=10)
polar.plot(testlen,testpos,main="Test Polar Plot",lwd=3,line.col=4)

You can then modify the parameters you pass to polar.plot to change the title, axes, etc.

Upvotes: 1

Related Questions