Home > other >  Plots of each DF column show the same Title (instead of the specific DF column name)
Plots of each DF column show the same Title (instead of the specific DF column name)

Time:06-17

Hi all!.

The R code below,
works great to display 4 plots,
one plot for each numeric column in the iris DF.
And that's exactly the plots i need.

Problem is,
all 4 plots (one for each column),
have the same default Title at the top:
Histogram of x

...instead of a plot Title at the top, for each specific DF column name:
Histogram of Sepal.Length
Histogram of Sepal.Width,
etc.

Here's the R code:

 f <- function(x){
      c( hist(x, freq = FALSE ), 
        lines(density(x)) )
    }
  sapply(iris[1:4], f) # where "f" is the FX f/above!.

What R-code am I missing?.
Help, please!...

SFer --
latest version of R, Rstudio, Ubuntu LINUX 20.04.

CodePudding user response:

par(mfrow = c(2,2))
f <- function(data, colname){
  hist(data[,colname], freq = FALSE, 
       main = paste("Histogram of", colname),
       xlab = colname) 
  lines(density(data[,colname]))
}

sapply(names(iris)[1:4], function(x) f(iris, x)) 

enter image description here

  • Related