如何替换`ggplot2::geom_histogram(`y = ..密度..`)`中的点-点表示法?

问题描述 投票:0回答:1

考虑这个基本的 ggplot2 示例:

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.4.0'
ggplot(mpg, aes(displ)) +
  geom_histogram(aes(y = ..density..))
#> Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
#> ℹ Please use `after_stat(density)` instead.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

点点表示法已弃用。所以我按照建议尝试了以下方法:

ggplot(mpg, aes(displ)) +
  geom_histogram(aes(y = after_stat(density)))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

但是在包内这会产生 R CMD 检查没有可见的绑定注释:

#> nice_density: no visible binding for global variable 'density'.

现在我知道可以使用

utils::globalVariables
定义全局变量或提前定义为
NULL
,但我知道这些不是首选解决方案。所以这是我的各种尝试:

ggplot(mpg, aes(displ)) +
  geom_histogram(aes(y = after_stat(stats::density)))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Error in `geom_histogram()`:
#> ! Problem while mapping stat to aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `map_statistic()`:
#> ! Aesthetics must be valid computed stats.
#> ✖ The following aesthetics are invalid:
#> ✖ `y = after_stat(stats::density)`
#> ℹ Did you map your stat in the wrong layer?

#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(input = base::quote("waspy-bunny_reprex.R"))
#>  13.   └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14.     └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15.       └─knitr:::process_file(text, output)
#>  16.         ├─base::withCallingHandlers(...)
#>  17.         ├─knitr:::process_group(group)
#>  18.         └─knitr:::process_group.block(group)
#>  19.           └─knitr:::call_block(x)
#>  20.             └─knitr:::block_exec(params)
#>  21.               └─knitr:::eng_r(options)
#>  22.                 ├─knitr:::in_input_dir(...)
#>  23.                 │ └─knitr:::in_dir(input_dir(), expr)
#>  24.                 └─knitr (local) evaluate(...)
#>  25.                   └─evaluate::evaluate(...)
#>  26.                     └─evaluate:::evaluate_call(...)
#>  27.                       ├─evaluate (local) handle(...)
#>  28.                       │ └─base::try(f, silent = TRUE)
#>  29.                       │   └─base::tryCatch(...)
#>  30.                       │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  31.                       │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  32.                       │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  33.                       ├─base::withCallingHandlers(...)
#>  34.                       ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  35.                       └─knitr (local) value_fun(ev$value, ev$visible)
#>  36.                         └─knitr (local) fun(x, options = options)
#>  37.                           ├─base::withVisible(knit_print(x, ...))
#>  38.                           ├─knitr::knit_print(x, ...)
#>  39.                           └─knitr:::knit_print.default(x, ...)
#>  40.                             └─evaluate (local) normal_print(x)
#>  41.                               ├─base::print(x)
#>  42.                               └─ggplot2:::print.ggplot(x)
#>  43.                                 ├─ggplot2::ggplot_build(x)
#>  44.                                 └─ggplot2:::ggplot_build.ggplot(x)
#>  45.                                   └─ggplot2:::by_layer(...)
#>  46.                                     ├─rlang::try_fetch(...)
#>  47.                                     │ ├─base::tryCatch(...)
#>  48.                                     │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  49.                                     │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  50.                                     │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  51.                                     │ └─base::withCallingHandlers(...)
#>  52.                                     └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  53.                                       └─l$map_statistic(d, plot)
#>  54.                                         └─ggplot2 (local) map_statistic(..., self = self)
#>  55.                                           └─cli::cli_abort(...)
#>  56.                                             └─rlang::abort(...)

ggplot(mpg, aes(displ)) +
  geom_histogram(aes_string(y = after_stat("density")))
#> Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
#> ℹ Please use tidy evaluation ideoms with `aes()`
#> Don't know how to automatically pick scale for object of type <function>.
#> Defaulting to continuous.
#> Error in `geom_histogram()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_aesthetics()`:
#> ! Aesthetics are not valid data columns.
#> ✖ The following aesthetics are invalid:
#> ✖ `y = density`
#> ℹ Did you mistype the name of a data column or forget to add `after_stat()`?

#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(input = base::quote("waspy-bunny_reprex.R"))
#>  13.   └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14.     └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15.       └─knitr:::process_file(text, output)
#>  16.         ├─base::withCallingHandlers(...)
#>  17.         ├─knitr:::process_group(group)
#>  18.         └─knitr:::process_group.block(group)
#>  19.           └─knitr:::call_block(x)
#>  20.             └─knitr:::block_exec(params)
#>  21.               └─knitr:::eng_r(options)
#>  22.                 ├─knitr:::in_input_dir(...)
#>  23.                 │ └─knitr:::in_dir(input_dir(), expr)
#>  24.                 └─knitr (local) evaluate(...)
#>  25.                   └─evaluate::evaluate(...)
#>  26.                     └─evaluate:::evaluate_call(...)
#>  27.                       ├─evaluate (local) handle(...)
#>  28.                       │ └─base::try(f, silent = TRUE)
#>  29.                       │   └─base::tryCatch(...)
#>  30.                       │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  31.                       │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  32.                       │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  33.                       ├─base::withCallingHandlers(...)
#>  34.                       ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  35.                       └─knitr (local) value_fun(ev$value, ev$visible)
#>  36.                         └─knitr (local) fun(x, options = options)
#>  37.                           ├─base::withVisible(knit_print(x, ...))
#>  38.                           ├─knitr::knit_print(x, ...)
#>  39.                           └─knitr:::knit_print.default(x, ...)
#>  40.                             └─evaluate (local) normal_print(x)
#>  41.                               ├─base::print(x)
#>  42.                               └─ggplot2:::print.ggplot(x)
#>  43.                                 ├─ggplot2::ggplot_build(x)
#>  44.                                 └─ggplot2:::ggplot_build.ggplot(x)
#>  45.                                   └─ggplot2:::by_layer(...)
#>  46.                                     ├─rlang::try_fetch(...)
#>  47.                                     │ ├─base::tryCatch(...)
#>  48.                                     │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  49.                                     │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  50.                                     │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  51.                                     │ └─base::withCallingHandlers(...)
#>  52.                                     └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  53.                                       └─l$compute_aesthetics(d, plot)
#>  54.                                         └─ggplot2 (local) compute_aesthetics(..., self = self)
#>  55.                                           └─cli::cli_abort(...)
#>  56.                                             └─rlang::abort(...)

ggplot(mpg, aes(displ)) +
  geom_histogram(aes_string(y = "density"))
#> Don't know how to automatically pick scale for object of type <function>.
#> Defaulting to continuous.
#> Error in `geom_histogram()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_aesthetics()`:
#> ! Aesthetics are not valid data columns.
#> ✖ The following aesthetics are invalid:
#> ✖ `y = density`
#> ℹ Did you mistype the name of a data column or forget to add `after_stat()`?

#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. └─global `<fn>`(input = base::quote("waspy-bunny_reprex.R"))
#>  13.   └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14.     └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15.       └─knitr:::process_file(text, output)
#>  16.         ├─base::withCallingHandlers(...)
#>  17.         ├─knitr:::process_group(group)
#>  18.         └─knitr:::process_group.block(group)
#>  19.           └─knitr:::call_block(x)
#>  20.             └─knitr:::block_exec(params)
#>  21.               └─knitr:::eng_r(options)
#>  22.                 ├─knitr:::in_input_dir(...)
#>  23.                 │ └─knitr:::in_dir(input_dir(), expr)
#>  24.                 └─knitr (local) evaluate(...)
#>  25.                   └─evaluate::evaluate(...)
#>  26.                     └─evaluate:::evaluate_call(...)
#>  27.                       ├─evaluate (local) handle(...)
#>  28.                       │ └─base::try(f, silent = TRUE)
#>  29.                       │   └─base::tryCatch(...)
#>  30.                       │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  31.                       │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  32.                       │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  33.                       ├─base::withCallingHandlers(...)
#>  34.                       ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  35.                       └─knitr (local) value_fun(ev$value, ev$visible)
#>  36.                         └─knitr (local) fun(x, options = options)
#>  37.                           ├─base::withVisible(knit_print(x, ...))
#>  38.                           ├─knitr::knit_print(x, ...)
#>  39.                           └─knitr:::knit_print.default(x, ...)
#>  40.                             └─evaluate (local) normal_print(x)
#>  41.                               ├─base::print(x)
#>  42.                               └─ggplot2:::print.ggplot(x)
#>  43.                                 ├─ggplot2::ggplot_build(x)
#>  44.                                 └─ggplot2:::ggplot_build.ggplot(x)
#>  45.                                   └─ggplot2:::by_layer(...)
#>  46.                                     ├─rlang::try_fetch(...)
#>  47.                                     │ ├─base::tryCatch(...)
#>  48.                                     │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  49.                                     │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  50.                                     │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  51.                                     │ └─base::withCallingHandlers(...)
#>  52.                                     └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  53.                                       └─l$compute_aesthetics(d, plot)
#>  54.                                         └─ggplot2 (local) compute_aesthetics(..., self = self)
#>  55.                                           └─cli::cli_abort(...)
#>  56.                                             └─rlang::abort(...)

那么官方推荐的新方法是什么? 一年前就有过类似的帖子,但没有得到回复。

全局变量“密度”没有可见绑定

创建于 2022 年 12 月 10 日,使用 reprex v2.0.2

r ggplot2 package histogram density-plot
1个回答
3
投票

您可以通过消除字符串作为符号参数来避免“无可见绑定”CMD nag:

library(ggplot2)

ggplot(mpg, aes(displ)) +
  geom_histogram(aes(y = after_stat(!!str2lang("density"))))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

创建于 2022 年 12 月 11 日,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.