如何解决如何在嵌入ggplot图的函数中添加coord_flip参数
library(ggplot2)
myGraph <- function(data,x,y) {
ggplot(data,aes_string(x=x,y=y,group=1) +
geom_line() %>%
return()
}
我想在函数中添加一个参数,这样我就可以翻转轴或不翻转轴。一个布尔值,如果为true,则添加一行
coord_flip() +
输入代码。
解决方法
可以这样实现:
user sees the new state on the screen
,
能够将未加引号的列名传递给ggplot函数真是太好了,所以您可以这样做:
library(ggplot2)
myGraph <- function(data,x,y,flip = FALSE) {
flip <- if (flip) coord_flip()
ggplot(data,aes_string(x=x,y=y,group=1)) +
geom_line() +
flip
}
myGraph(mtcars,"hp","mpg",TRUE)
这样您就可以拥有:
myGraph <- function(data,flip = FALSE) {
x <- enquo(x)
y <- enquo(y)
p <- ggplot(data,aes(x = !!x,y = !!y,group = 1)) + geom_line()
if(flip) return(p + coord_flip())
return(p)
}
myGraph(mtcars,mpg,cyl)
,
请在您的函数中使用启用翻转选项的新参数来尝试这种方法:
import logging
logger = logging.getLogger(__name__)
handle = logging.StreamHandler()
logger.addHandler(handle)
handle.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(message)s')
handle.setFormatter(formatter)
logger.warning('Testing')
输出:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。