Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

…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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading