r语言 - 数据帧中不同变量的循环 ggplot



我想循环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)
}

问题出在哪里?

我知道我可以使用facet_grid.

ggplot(df_melt, aes(x=ID,y=value)) + geom_bar(stat="identity") + facet_grid(~variable)
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)
}

最新更新