0

I have created some code to generate histograms using apply on the state.x77 dataset.

attach(as.data.frame(state.x77))
lapply(X=c("Population","Income","Illiteracy","Life Exp","Murder","HS Grad","Frost","Area"),FUN=function(s)hist(state.x77[,s],main=paste("Hist of",s)))

It works fine with generating the titles for the graphs but I do not know how to create labels for the x and y-axis.

The y-axis has the label "frequency" which is fine but I want to make it so that each x-axis is labelled according to the corresponding variable in state.x77.

How would I generate the labels?

JD kreyfelt
  • 125
  • 7
  • 1
    [Don't](https://stackoverflow.com/questions/10067680/why-is-it-not-advisable-to-use-attach-in-r-and-what-should-i-use-instead) [`attach`](https://stackoverflow.com/questions/43130086/trouble-using-attach-in-r), [please](https://stackoverflow.com/questions/40242904/problems-with-attach-in-r/40243225). – Rui Barradas Apr 11 '21 at 14:45
  • What is wrong with using `attach`? – JD kreyfelt Apr 11 '21 at 14:57
  • Those are links to **3** questions on that subject. – Rui Barradas Apr 11 '21 at 15:45

1 Answers1

0

I found the answer

lapply(X=c("Population","Income","Illiteracy","Life Exp","Murder","HS Grad","Frost","Area"),FUN=function(s)hist(state.x77[,s],main=paste("Hist of",s),xlab=s))

Adding xlab=s works for the labels.

JD kreyfelt
  • 125
  • 7