繁体   English   中英

使用ggplot2的圆形密度图

[英]Circular density plot using ggplot2

我正在使用循环数据,我想使用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. 最尖锐的箭头是我间隔的极端
  2. 我想预测蓝点
  3. 第三个箭头是圆形平均值
  4. 我需要圆密度

我一直在寻找类似的东西,但我没有找到任何东西。 有什么建议吗?

谢谢

为了避免错误的方向运行,您会快速检查此代码是否朝着正确的方向运行? 可以使用+箭头(...)轻松添加箭头并加载适当的箭头。

编辑:关于附加密度值的复杂方法的一个评论 - 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)采样的均匀随机变量:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM