我试图把一个论点作为一个角色传递给ggvis,但我得到了一个空洞的情节。
可复制的例子:
library(ggvis)
y <- c("mpg", "cyl")
ing <- paste0("x = ~ ", y[1], ", y = ~ ", y[2])
#works as intended
mtcars %>% ggvis(x = ~ mpg, y = ~ cyl) %>%
layer_points()
#gives empty plot
mtcars %>% ggvis( ing ) %>%
layer_points()这与工作正常的lm()中的以下方法有何不同?
formula <- "mpg ~ cyl"
mod1 <- lm(formula, data = mtcars)
summary(mod1)
#works谢谢
发布于 2015-11-10 15:15:18
在lm情况下,字符串将在内部被胁迫到类公式对象。创建这个公式对象的是~操作符。
在第二种情况下,ggvis需要两个单独的x和y参数公式。在您的情况下,只有一个长字符串,如果在逗号上拆分,可以将其强制分成两个单独的公式(但这个长字符串本身并不是一个公式)。
因此,为了工作,ggvis函数必须是这样的:
#split the ing string into two strings that can be coerced into
#formulas using the lapply function
ing2 <- lapply(strsplit(ing, ',')[[1]], as.formula)
#> ing2
#[[1]]
#~mpg
#<environment: 0x0000000035594450>
#
#[[2]]
#~cyl
#<environment: 0x0000000035594450>
#use the ing2 list to plot the graph
mtcars %>% ggvis(ing2[[1]], ing2[[2]]) %>% layer_points()但这不是一件很有效率的事。
https://stackoverflow.com/questions/33633069
复制相似问题