使用ggplot2的圆形密度图 [英] Circular density plot using ggplot2

查看:179
本文介绍了使用ggplot2的圆形密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理循环数据,我想用ggplot2重现这种情节:

  library(circular) 

data1 < - rvonmises(1000,circular(0),10,control.circular = list(units =radians))## sample
quantile.circular(data1,c (0.05,.95))##对于区间

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 =圆形密度,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)##循环均值




  1. 最简单的箭头是我的间隔极限

  2. 我想蓝点是预测的

  3. 第三个箭头是圆形平均值

  4. 我需要循环密度

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



谢谢

解决方案

快速检查这段代码是否朝着正确的方向走?使用+箭头(...)可以很容易地添加箭头,并带有适当的加载。

编辑:附加密度值的复杂方式的一种说法 - ggplot的geom_density不会似乎喜欢coord_polar(至少是我试过的方式)。

 #创建一些假的径向数据并将其包装在数据框中
d1< -runif(100,min = 0,max = 120)
df = NULL
df $ d1 < - d1
df < - as.data.frame (df)

##核心密度,然后导出一个近似函数来将密度值附加到数据帧中的径向值
data_density < - density(d1)
density_function< ; - with(data_density,approxfun(x,y,rule = 1))
df $ density < - density_function(df $ d1)

#排列数据框以方便geom_line在极坐标
df < - df [order(df $ density,df $ d1),]

#ggplot object
require(ggplot2)
g = ggplot(df, aes(x = d1,y =密度))
#单位圆上的径向观测值
g = g + geom_poi nt(aes(x = d1,y = min(df $ density)))
#Density函数
g = g + geom_line()
g = g + ylim(0,max(df $密度))
g = g + xlim(0,360)
#极坐标
g = g + coord_polar()
g

从(0,120)抽样的统一随机变量:


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

解决方案

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):

这篇关于使用ggplot2的圆形密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆