4

I'm working with circular data and I wanted to reproduce this kind of plot using ggplot2:

library(circular)

data1 <- rvonmises(1000, circular(0), 10, control.circular=list(units="radians")) ##        sample
quantile.circular(data1,c(0.05,.95)) ## for interval

data2 <- mean(data1)
dens <- density(data1, bw=27)
p<-plot(dens, points.plot=TRUE, xlim=c(-1,2.1),ylim=c(-1.0,1.2),
main="Circular Density", ylab="", xlab="")
points(circular(0), plot.info=p, col="blue",type="o")
arrows.circular(c(5.7683795,0.5151433    )) ## confidence interval
arrows.circular(data2, lwd=3) ## circular mean
  1. The thinest arrows are extremes of my interval
  2. I suppose blue point is forecast
  3. The third arrow is circular mean
  4. I need circular density

I've been looking for something similar but I did not found anything. Any suggestion?

Thanks

Uzg
  • 309
  • 2
  • 4
  • 9
  • See `?coord_polar`. I'm not familiar with the `circular` data formats, but you can probably get something to work. – Gregor Thomas Sep 25 '14 at 06:00
  • You can also enforce the circular nature of the data without using polar coordinates. ([Example in ggplot](https://stackoverflow.com/questions/36266402/ggplot2-density-of-circular-data)) ([Example in base R](http://rstudio-pubs-static.s3.amazonaws.com/3369_998f8b2d788e4a0384ae565c4280aa47.html)). – filups21 Dec 06 '20 at 18:14

1 Answers1

2

To avoid running in the wrong direction would you quickly check if this code goes in the right direction? The arrows can be added easily using +arrow(...) with appropriate loading.

EDIT: One remark to the complicated way of attaching density values - ggplot's geom_density does not seem to like coord_polar (at least the way I tried it).

#create some dummy radial data and wrap it in a dataframe
d1<-runif(100,min=0,max=120)
df = NULL
df$d1 <- d1
df <- as.data.frame(df)

#estimate kernel density and then derive an approximate function to attach density values to the radial values in the dataframe
data_density <- density(d1)
density_function <- with(data_density, approxfun(x, y, rule=1))
df$density <- density_function(df$d1)

#order dataframe to facilitate geom_line in polar coordinates
df <- df[order(df$density,df$d1),]

#ggplot object
require(ggplot2)
g = ggplot(df,aes(x=d1,y=density))
#Radial observations on unit circle
g = g + geom_point(aes(x=d1,y=min(df$density)))
#Density function
g = g + geom_line()
g = g + ylim(0,max(df$density))
g = g + xlim(0,360)
#polar coordinates
g = g + coord_polar()
g

Uniform random variables sampled from (0,120):

enter image description here

CMichael
  • 1,856
  • 16
  • 20
  • Just another question, can I set coord_polar in radians? – Uzg Sep 25 '14 at 06:51
  • Looking at the documentation it geom_coord actually expects radians. In my code you could just create the random variable over [0,pi] and then set the xlim(0,2*pi) to illustrate this. I have always personally struggled with radians and polar coordinates but coord_polar is very forgiving on the input :) – CMichael Sep 25 '14 at 06:56
  • 1
    Regarding the trouble with `stat_density`, ggplot's polar coordinates implementation really just is a transform of a standard cartesian plot. None of the stat_*'s "know" that your data is circular. You can still plot most anything, but you need to do any computations beforehand and explicitly pass ggplot the coordinates for plotting. – Gregor Thomas Sep 25 '14 at 15:45