r - 使用 ggplot2 绘制圆形密度图

标签 r ggplot2 geometry

我正在处理循环数据,我想使用 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) 采样的均匀随机变量:

    enter image description here

    关于r - 使用 ggplot2 绘制圆形密度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030244/

    相关文章:

    R ggplot2图例里面的图

    r - 对 R 中的一系列值求和(不使用 for 循环)

    r - ggplot2:将线宽应用于图例键

    r - 堆积条形图 : what if each subcategory (fill) is unique?

    r - 在 R 中动态对齐图(空间图的自定义 ggplot2 图例)

    html - 在数据框列上使用 rvest 函数

    r - r-ggplot2-突出显示选定的点和奇怪的行为

    javascript - 创建 JavaScript 倒计时

    algorithm - 往返经纬度和3D点的可逆算法

    php PDO 和 mysql : how to insert a geo point type?