2017-07-09 3 views
1

ggplotをループし、各変数コードのグラフを取得する必要があります。私は、問題は何aesデータフレーム内の異なる変数のループggplot

df <- data.frame(ID = c("a", "b"), A = rnorm(2), B = runif(2), C = rlnorm(2)) 
for(i in df[,2:ncol(df)]){ 
plt<-ggplot(data=df, aes(x=df$ID, y = df[,2:ncol(df)]$i))+ 
    geom_bar() 
    print(plt) 
} 

でエラーが出ますか?

facet_gridを使用することができます。あなたは今、 `` geom_colによって `geom_bar(STAT = "アイデンティティ")を置き換えることができ

ggplot(df_melt, aes(x=ID,y=value)) + geom_bar(stat="identity") + facet_grid(~variable) 

答えて

3
df <- data.frame(ID = c("a", "b"), A = rnorm(2), B = runif(2), C = rlnorm(2)) 

for (colname in names(df)[-1]) { 
    plt <- ggplot(data = df, aes_string("ID", y = colname)) + 
    geom_bar(stat = "identity") 
    print(plt) 
} 
+0

注()'。 –

関連する問題