0

I am trying to plot the histogram for all the variables for the Wage dataset in order to understand their distribution. This I am trying to achieve by passing i column index as parameter inside a for loop in R, using following code.

library(ISLR); 
library(ggplot2);
# load wage data
data(Wage)
par(mfrow=c(3,3))
for(i in 1:9){
  ggplot(data=Wage)+geom_bar(aes(x=Wage[,i]))
}

But I am unable to produce any plot (this is error free). It is working if I individually provide the column numbers. I suspect the problem is somewhere in the par() or the for line. Kindly requesting to suggest.

Aditi
  • 820
  • 11
  • 27
Scott Grammilo
  • 1,229
  • 4
  • 16
  • 37
  • Try `aes_string(x = names(Wage)[i])` – Gregor Thomas Oct 24 '17 at 05:04
  • Oh `aes_string(x = names(Wage)[i])` didn't work – Scott Grammilo Oct 24 '17 at 14:27
  • It works fine, you just have other issues as well. `par` works for base graphics, not `ggplot`, and you need to explicitly `print` ggplots inside loops. See [Side-by-side plots with ggplot](https://stackoverflow.com/q/1249548/903061) for fixing the par issue. If you want to see it work without that, delete the `par` line and put `print(ggplot(data=Wage)+geom_bar(aes_string(x=names(Wage)[i])))` in the loop. – Gregor Thomas Oct 24 '17 at 14:39

0 Answers0