Uzg
Uzg

Reputation: 319

Circular density plot using ggplot2

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

Upvotes: 4

Views: 2753

Answers (1)

CMichael
CMichael

Reputation: 1866

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

Upvotes: 2

Related Questions