循环ggplot数据框中的不同变量

Al1*_*l14 1 loops r ggplot2

我想循环 ggplot 并获取每个变量的图表,代码如下。我收到错误aes

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)
}
Run Code Online (Sandbox Code Playgroud)

问题是什么?

我知道我可以使用facet_grid.

ggplot(df_melt, aes(x=ID,y=value)) + geom_bar(stat="identity") + facet_grid(~variable)
Run Code Online (Sandbox Code Playgroud)

F. *_*ivé 5

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)
}
Run Code Online (Sandbox Code Playgroud)