0

I'm interested in Using functions and lapply to make properly labelled histograms.

But when I try to use a function and lapply to create histograms that display the spread of data, the xlab doesn't give the text of the variable of interest. Instead it uses the first value of the variable of interest. How to I fix this issue?

The code I used is below:



# loads ggplot2 package
if(!require(ggplot2)){install.packages("ggplot2")} # ---- NOTE: relates to mlm analyses

# creates function_dataset_diamonds_DV_histogram
function_dataset_diamonds_DV_histogram <-
  function(
    DV_use
  )
  {
    ggplot(diamonds, aes(x=as.numeric(DV_use))) +
      geom_histogram(aes(y=..density..), alpha=0.5,
                     position="identity", binwidth = nclass.Sturges(DV_use)) +
      geom_density(alpha=.2) +
      theme(legend.position="right") +
      xlab(DV_use)
  }

# tests function
function_dataset_diamonds_DV_histogram(diamonds$carat)

# applies function
lapply((diamonds[c(1,8:10)]), function_dataset_diamonds_DV_histogram)

Mel
  • 510
  • 3
  • 10
  • Do you mean that your current code is not retrieving the name of the column you are interested in? If so, then you cannot succeed with `lapply`, since that function does not pass the name of its first argument elements to functions. This answer might give you more to "chew on".https://stackoverflow.com/questions/10520772/in-r-how-to-get-an-objects-name-after-it-is-sent-to-a-function/10520832?r=SearchResults&s=2|120.8731#10520832 – IRTFM Mar 12 '21 at 21:39

1 Answers1

2

You're passing the data vector to xlab so it just truncates it to the first value. You want to pass a string.

Modify your function to take a label value and then use mapply

function_dataset_diamonds_DV_histogram <- function(DV_use, xLabel){

  
  ggplot(diamonds, aes(x=as.numeric(DV_use))) +
    geom_histogram(aes(y=..density..), alpha=0.5,
                   position="identity", binwidth = nclass.Sturges(DV_use)) +
    geom_density(alpha=.2) +
    theme(legend.position="right") +
    xlab(xLabel)
}

mapply(function_dataset_diamonds_DV_histogram, diamonds[c(1,8:10)], names(diamonds[c(1,8:10)]), SIMPLIFY = FALSE)
Marcus
  • 3,478
  • 1
  • 7
  • 16