You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2中geom_area超出绘图范围部分不显示:预期行为还是Bug?

ggplot2中ylim(0,1)导致部分区域不显示:Bug还是预期行为?

我在以下代码中发现,名为gg2的绘图并未显示所有区域,对此我感到疑惑。我猜测这是数值精度问题结合ylim参数导致的,但仍不确定。请问这是Bug还是预期行为?

感谢您的解答,
Sebastian

library('ggplot2')
library('magrittr')
library('tibble')
library('dplyr')
library('tidyr')

squeeze <- function(x, min_value = 0, max_value = 1) {
  pmin(max_value, pmax(min_value, x))
}

par_values <- 0.1 * 2^(-3:3)

result <- expand_grid(beta = par_values, 
                      gamma = par_values) %>%
  mutate(S = squeeze(gamma / beta),
         I = squeeze((beta - gamma) / beta))

result_long <- result %>% pivot_longer(cols = c('S', 'I'))

(gg1 <- result_long %>%
    ggplot(aes(x = beta, y = value, fill = name)) + geom_area() +
    facet_grid(rows = vars(gamma)))

(gg2 <- gg1 + ylim(0, 1))

(gg3 <- gg1 + ylim(0, 1.001))

Session Info

R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.2

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] tidyr_1.2.1    dplyr_1.0.10   tibble_3.1.8   magrittr_2.0.3 ggplot2_3.4.0

loaded via a namespace (and not attached):
[1] rstudioapi_0.14  tidyselect_1.2.0 munsell_0.5.0    colorspace_2.0-3 R6_2.5.1         rlang_1.0.6      fansi_1.0.3      tools_4.2.1      grid_4.2.1       gtable_0.3.1     utf8_1.2.2
[12] cli_3.6.0        DBI_1.1.3        withr_2.5.0      ellipsis_0.3.2   assertthat_0.2.1 lifecycle_1.0.3  farver_2.1.1     purrr_1.0.1      vctrs_0.5.1      glue_1.6.2       labeling_0.4.2
[23] compiler_4.2.1   pillar_1.8.1     generics_0.1.3   scales_1.2.1     pkgconfig_2.0.3

内容的提问来源于stack exchange,提问作者Sebastian Gerdes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 23:46:01