2016-12-19 18 views
1

私のデータセット内のいくつかのy変数のこのタイプのボックスプロットを入力してください:normal boxplot for all irises with Species as x-value。私はプロットするために、複数の変数yを持っているので、私はこのようなlapply使用しようとした: with only one iris per plotlapplyを使用して変数リストのボックスプロットを作成する

私が(理由は、いくつかのループのタイプを使用して、通常の箱ひげ図を取得できます:私はこのプロットを得た

varlist <- c('Sepal.Length', 'Sepal.Width') 

plot <- function (varlist) { 
    require(ggplot2) 
    ggplot(data = iris, aes(x=Species, y=varlist))+ 
    geom_boxplot() 
} 

lapply(varlist, FUN = plot) 

y値)、x変数でグループ化されたすべての虹彩がボックスに含まれている場所は?

答えて

1

IIRC、aes()は文字列入力を処理しません。 aes_string()が必要です。私は、の電話番号をggplot(data = iris, mapping = aes_string(x = 'Species', y = varlist))に変更した場合、あなたの機能が動作することを期待しています(しかし、テストしていません)。あなたができるdplyr

0

library("ggplot2") 
library("dplyr") 


varlist <- c('Sepal.Length', 'Sepal.Width') 

customPlot <- function(varName) { 

iris %>% 
group_by_("Species") %>% 
select_("Species",varName) %>% 
ggplot(aes_string("Species",varName)) + geom_boxplot() 

} 
lapply(varlist,customPlot) 

プロット:

enter image description here

enter image description here

はまたplotが一般plotting.Itための基本機能ではないことに注意ベースフを上書きするのに安全後で予期しない結果につながる可能性があるため、ユーザー定義関数を使用してください。

関連する問題