我有一个包含 2 个变量的 464 个观测值的数据集(“mdata”)。 x 变量是时间间隔,y 变量是每个时间间隔的测量值。我正在使用 mcp 寻找间隔 0 到 3000 之间的某个变化点,它已在“1500”附近识别出该变化点。在这种情况下。到目前为止一切顺利。
我想做的是以仅显示感兴趣区域的方式绘制这个变化点(例如,从 0 到 5000 的范围),但我无法让绘图停止显示整个 0-15000 范围x 刻度。
mcp 绘图尝试显示 y 轴如何受到限制(使用 ylim),但 x 轴保持不变 - 尽管使用 xlim
m数据: |水平|间隔 | | -------- | -------- | | .5 | 300 | 300 | 1.3 | 1.3 5000 |
我第一次尝试:
绘图(fit_mcp)+ theme_minimal()
然后,我尝试使用以下方法限制 x 和 y 轴:
绘图(fit_mcp)+ theme_minimal()+ xlim(0,5000)+ ylim(0,10)
y 轴更改为显示在 0 到 10 之间,但 x 轴保持在 0 到 15000 之间。我怀疑 mcp 图中有一些代码告诉 ggplot2 绘制整个 x 范围,但我无法弄清楚如何改变它。
使用
ggplot2::coord_cartesian()
调整限制而不改变正在绘制的数据。图表下方 reprex 的数据生成。
plot(fit)
使用
coord_cartesian
放大变更点:
plot(fit) + coord_cartesian(xlim = c(50, 80))
示例取自
mcp
文档。
library(mcp)
library(ggplot2)
set.seed(42)
example <- mcp_example('demo')
model <- list(
response ~ 1,
~ 0 + time,
~ 1 + time
)
fit <- mcp(model, data = example$data)
#> Compiling model graph
#> Resolving undeclared variables
#> Allocating nodes
#> Graph information:
#> Observed stochastic nodes: 100
#> Unobserved stochastic nodes: 7
#> Total graph size: 1732
#>
#> Initializing model
#> Finished sampling in 5.4 seconds