取消列名并在 facet_grid 中使用它
Unlist column names and using it in facet_grid
我有一长串列名称,我想在 facet_grid
中使用它。我想 select 一些列名并将 +
作为 facet_grid
函数要求。
我已经用 mtcars
试过了,但无法弄清楚为什么 unlist
或 paste
不起作用。
names=paste(unlist(names(mtcars)[c(1,3,5)]),sep='+')
library(ggplot2)
ggplot(mtcars, aes("", hp)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_grid(. ~ names,switch = 'both',labeller = label_both)
Error in combine_vars(data, params$plot_env, cols, drop = params$drop)
: At least one layer must contain all variables used for facetting
您可以在 facet_grid
中使用字符串,但您必须以字符串形式提供整个公式(包括波浪号)。
names <- paste(c(". ~ ", names(mtcars)[c(1,3,5)]), collapse='+')
ggplot(mtcars, aes("", hp)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_grid(names, switch = 'both', labeller = label_both)
我有一长串列名称,我想在 facet_grid
中使用它。我想 select 一些列名并将 +
作为 facet_grid
函数要求。
我已经用 mtcars
试过了,但无法弄清楚为什么 unlist
或 paste
不起作用。
names=paste(unlist(names(mtcars)[c(1,3,5)]),sep='+')
library(ggplot2)
ggplot(mtcars, aes("", hp)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_grid(. ~ names,switch = 'both',labeller = label_both)
Error in combine_vars(data, params$plot_env, cols, drop = params$drop) : At least one layer must contain all variables used for facetting
您可以在 facet_grid
中使用字符串,但您必须以字符串形式提供整个公式(包括波浪号)。
names <- paste(c(". ~ ", names(mtcars)[c(1,3,5)]), collapse='+')
ggplot(mtcars, aes("", hp)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_grid(names, switch = 'both', labeller = label_both)