r-无法动态准备ggplot语句

  • 本文关键字:ggplot 语句 动态 r ggplot2
  • 更新时间 :
  • 英文 :


我正在尝试使用下面的函数动态地准备ggplot语句。这样我就可以选择我的Geom类型(Geom_point((、Geom_jitter((、Geom_bar((等(基于我从闪亮的网络应用程序的数据属性。我现在面临的问题是";graph_type";由于该原因,它将解析为带双引号的字符串。有没有什么方法可以从输入值中删除双引号,使选择动态?。

func_ggplt <-
function(xAxis,
yAxis,
aes_color = NULL,
graph_type,
title = NULL,
xAxis_label = NULL,
yAxis_label = NULL) {
p1 <- ggplot(ggplt_data, aes_string(xAxis, yAxis, color = aes_color))
graph_type <- as.name(graph_type)
p2 <- graph_type
p3 <- geom_smooth()
p4 <- ggtitle(title)
p5 <- xlab(xAxis_label)
p6 <- ylab(yAxis_label)
p1 + p2 + p3 + p4 + p5 + p6
}

下面是我正在尝试的函数的输入值。

func_ggplt('TCM.Deal.net.USD.M_dollar','TCM.Total.Quantity.M_dollar',NULL,"geom_point()",'MyTest','TCMUSD','TCMQTY')

有几种方法可以做到这一点,但最简单的方法是将geom_type添加到参数调用中,不带撇号,如下所示:

ggtest <- function(test) {
geom_type = eval(parse(text = test))
summarized_df %>% ggplot(aes(n, n_image)) + geom_type
}
ggtest("geom_point()")

最新更新