r - 使用 ggplot2::stat_y Density 绘制横向密度

标签 r ggplot2

我一直在使用 geom_violin 的修改版本制作如下图所示的图形:

Plot with modified version of geom_violin

但是,我意识到这个图可能可以直接使用stat_y密度来实现。不幸的是,我无法让它正常工作:

ggplot(res.plot, aes(x = time, y = x,group = run)) + theme_bw() +
  geom_line(alpha = 0.1) +
  stat_ydensity(aes(group = time), data = res.select, 
    geom = "density", color = "red")

failed attempt with stat_ydensity

我尝试过使用geom =“path”geom =“line”,并尝试了不同的位置,例如position = "identity"position =position_dodge(width = 2),但无法生成类似于顶部图的任何内容。

谁能解释一下我做错了什么?下面的 res.plotres.select 数据框的代表:

library(tidyr)
library(ggplot2)
library(dplyr)
# markov chain parameters
mu = 8                           # cm/hr
sigma = 4                        # cm/sqrt(hr)
x0 = 3                           # initial condition
tmax = 200                       # end time
deltat = 10                      # time increment (hrs)
reps = 300                       # number of realizations

random_walk = function() 
  c(0, cumsum(mu*deltat + sigma*rnorm(n, sd = deltat))) + x0

# simulate random walks
n = tmax/deltat
res = cbind.data.frame(seq(0,tmax, by = deltat), replicate(reps, random_walk()))
names(res) = c("time", paste("run", seq(1, ncol(res) - 1)))
# format the data for plotting
res.plot = gather(res, run, x, -time)
# extract specific times to compute marginal densities
res.select = filter(res.plot, time %in% c(50, 150))

最佳答案

这不是一个“答案”,因为它不能解决问题,但它可能有助于OP理解为什么它不起作用:

ggplot(res.plot, aes(x = time, y = x, group = run)) + theme_bw() +
  geom_line(alpha = 0.1) +
  stat_ydensity(aes(group = time), data = res.select, 
    geom = "density", color = "red") -> gg

gb <- ggplot_build(gg)

str(gb$data[[2]])
## 'data.frame': 1024 obs. of  20 variables:
##  $ x          : num  50 50 50 50 50 50 50 50 50 50 ...
##  $ density    : num  0.000139 0.000144 0.000149 0.000154 0.000159 ...
##  $ scaled     : num  0.0308 0.0319 0.033 0.0342 0.0354 ...
##  $ ndensity   : num  0.0308 0.0319 0.033 0.0342 0.0354 ...
##  $ count      : num  0.0417 0.0432 0.0447 0.0462 0.0478 ...
##  $ n          : int  300 300 300 300 300 300 300 300 300 300 ...
##  $ y          : num  178 179 180 181 182 ...
##  $ group      : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ PANEL      : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ violinwidth: num  0.0308 0.0319 0.033 0.0342 0.0354 ...
##  $ ymin       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ ymax       : num  178 179 180 181 182 ...
##  $ xmin       : num  50 50 50 50 50 50 50 50 50 50 ...
##  $ xmax       : num  50 50 50 50 50 50 50 50 50 50 ...
##  $ colour     : chr  "red" "red" "red" "red" ...
##  $ fill       : chr  "#617a89" "#617a89" "#617a89" "#617a89" ...
##  $ weight     : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ alpha      : logi  NA NA NA NA NA NA ...
##  $ size       : num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
##  $ linetype   : num  1 1 1 1 1 1 1 1 1 1 ...

查看密度的值(及其同级)。这是计算出的密度,并且 - 请记住 - 密度图曲线下的面积 == 1。stat_ydensis() 中没有像 geom_densis() 那样的选项做数学计算并为您计算结果。

您可以尝试采用gb$data[[2]],重新计算路径的点并稍后手动添加。但是,保留本地 geom 并关注 ggplot2 版本以在必要时进行更新也没有什么问题。我一直这样做。

关于r - 使用 ggplot2::stat_y Density 绘制横向密度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52862039/

相关文章:

r - 在 R 中对数据框进行排序(基于列值)

r - 计算最大直方图值

r - 在分组箱线图上放置水平线

r - 在定义中使用省略号时如何在 R 函数调用中捕获错误或未定义的参数

r - 在 R 中交织两个 data.frames

r - 使用线条和多因子连接 ggplot 箱线图

r - ggplot geom_point随窗口大小变化大小

r - R 中随机数的总和

r - 如何协调 facet_wrap 中的轴并缩放 ="free_y"?

r - ggplot2 色标受异常值影响过度