简体   繁体   English

如何在不加载包的情况下使用`ggplot2`的功能?

[英]How to use `ggplot2`'s functions without loading the package?

When using ggplot2 , sometimes we need to use multiple functions in order to plot data:使用ggplot2 ,有时我们需要使用多个函数来绘制数据:

library("ggplot2")
p <- ggplot(mpg) + 
  geom_bar(aes(x = .data$drv)) + 
  coord_flip()

An alternative way to do this plot without loading the whole ggplot2 package would be:在不加载整个ggplot2包的情况下执行此绘图的另一种方法是:

p <-ggplot2::ggplot(ggplot2::mpg) + 
   ggplot2::geom_bar(ggplot2::aes(x = .data$drv)) + 
   ggplot2::coord_flip()

How can we do this plot without having to load the package throught library("ggplot2") , or having to write ggplot2:: for every function?我们如何在不必通过library("ggplot2")加载包或不必为每个函数编写ggplot2::情况下绘制此图?

withr::with_package temporarily loads a package. withr::with_package临时加载一个包。

p <- withr::with_package("ggplot2", {
  ggplot(mpg) + 
    geom_bar(aes(x = drv)) + 
    coord_flip()
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM