2017-08-04 8 views
2

facet_gridで使用する列名のリストがあります。いくつかの列名を選択し、+facet_grid function requiredとしたいと思います。列名をリストから削除してfacet_gridで使用する

私はmtcarsでこれを試しましたが、なぜunlistまたはpasteが機能していないのか理解できませんでした。 combine_varsで

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) 

エラー (データは、$ plot_env、COLS、=ののparams $ドロップをドロップparamsは):あなたが使用することができます

答えて

3

を面取りするために使用されるすべての変数が含まれている必要があり、少なくとも一つの層文字列内にはが含まれていますが、文字列として全体の式(チルダを含む)を指定する必要があります。

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) 

enter image description here

+0

おかげブライアン。あなたのソリューションはエレガントです! – Alexander

関連する問題