rplothistogramlapply

plotting the individual histograms after lapply


I am having a problem plotting the individual histograms after using lapply on a data table. The instruction plots all the histograms but I need access to them individually. All the histograms objects are available in a list but when I access (for example) the last histogram object and try to plot it , errors arise. The code is shown below.

I use the mt cars data set and form a table from it.

library(data.table)
mtcars$carname <- rownames(mtcars)
mtcars_dt <- data.table(mtcars)
histograms<- mtcars_dt[, lapply(.SD[, 1:10, with=F], hist), by=cyl]
# The above will plot all 30 data frames ie 10 for each value of cyl (3 of) 
# I now want to plot a single histogram, lets say the 10th one
b=histograms[[10]] # this is a histogram object
plot(b) # try to plot the object 
#
# Result is the error shown below:

# Error in if (freq && !equidist) warning("the AREAS in the plot are wrong -      
#- rather use 'freq = FALSE'") : 
#missing value where TRUE/FALSE needed
# In addition: Warning messages:
# 1: In min(x, na.rm = na.rm) :
# no non-missing arguments to min; returning Inf
# 2: In max(x, na.rm = na.rm) :
##no non-missing arguments to max; returning -Inf
#3: In mean.default(h) : argument is not numeric or logical: returning NA

I have plotted histogram object before but I notice that when I do a str on this object it has many more elements in the list than a simple histogram . The last element is an attr element with the word histogram next to it. I am not sure if I need to access that element or hoe to access it.


Solution

  • I'm no data.table expert. But I would do something like this using ggplot2 for plotting:

    First create a function which plots three histograms per cyl group

    foo <- function(x){
     require(ggplot2)
     ggplot(mtcars, aes(x = x)) + geom_histogram() + facet_grid(. ~ cyl)
    }
    

    lapply function over columns, save plots in a list

    histograms <- lapply(mtcars, foo)
    

    Now you can access individual plots by

    histograms[3]
    # or
    histograms$disp
    

    enter image description here