【问题标题】:Circular density plot using ggplot2使用 ggplot2 的圆形密度图
【发布时间】:2014-11-19 17:37:32
【问题描述】:

我正在处理循环数据,我想使用 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. 我需要圆形密度

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

谢谢

【问题讨论】:

  • ?coord_polar。我不熟悉 circular 数据格式,但您可能会得到一些工作。
  • 你说得对,谢谢
  • 您还可以在不使用极坐标的情况下强制数据的圆形性质。 (Example in ggplot) (Example in base R)。

标签: r ggplot2 geometry


【解决方案1】:

为了避免跑错方向,你会快速检查这段代码是否走对了方向吗?可以使用 +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) 中采样的均匀随机变量:

【讨论】:

  • 还有一个问题,我可以将 coord_polar 设置为弧度吗?
  • 查看文档 geom_coord 实际上需要弧度。在我的代码中,您可以在 [0,pi] 上创建随机变量,然后设置 xlim(0,2*pi) 来说明这一点。我个人一直在与弧度和极坐标作斗争,但 coord_polar 对输入非常宽容:)
  • 关于stat_density 的问题,ggplot 的极坐标实现实际上只是标准笛卡尔图的转换。没有一个 stat_* 的“知道”您的数据是循环的。您仍然可以绘制大多数任何东西,但您需要事先进行任何计算并显式传递 ggplot 用于绘图的坐标。
  • 感谢 Gregor 的澄清!
猜你喜欢
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多