我正在处理循环数据,我想使用 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
我一直在寻找类似的东西,但我没有找到任何东西。
有什么建议吗?
谢谢
最佳答案
为了避免朝错误的方向运行,您是否会快速检查此代码是否朝正确的方向运行?可以使用 +arrow(...) 以适当的加载轻松添加箭头。
编辑:对附加密度值的复杂方式的一个评论 - ggplot 的 geom_density 似乎不喜欢 coord_polar(至少我尝试过的方式)。
#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
从 (0,120) 采样的均匀随机变量:
关于r - 使用 ggplot2 绘制圆形密度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030244/