# One by one making a histogram out each column of a dataframe

I have a table that looks like this. (In reality, there are over a million rows and 70-something columns)

ind1 ind2 ind3 ... indn
0.1 0.2 0.3 0.4 0.5
1.0 0.9 0.8 0.7 0.6
1.0 1.0 1.0 1.0 1.0
0.9 0.9 0.9 0.9 0.9


I want an automated procedure to create a histogram for every column in the table. This is the code I have, but it doesn’t work

for (i in 1:10){
plN <- ggplot(cdf, aes(x=colnames(cdf)[i])) + geom_histogram(binwidth = 0.01)
plot(plN)
}


How do I extract the column name for the ith column of the dataframe, so I can plug that in in place of x=…? The current setup doesn’t work.

(Answered in Duck’s comment below. Thanks for the help!)

Stack Overflow Asked by dnv89 on November 15, 2021

I also like writing single functions and then using a loop to call the function.

I'll create your dataset with some random numbers.

library(tidyverse)
set.seed(123)
cdf <- data.frame(
A = rnorm(1000, -3, 1),
B = rnorm(1000, 1, 1),
C = rnorm(1000, 5, 10),
D = rnorm(1000, -3, 2)
)


Then write a function to graph a given dataset and function. I also include whether to print it.

myhist_function <- function(mydata, mycolumn, printit = F) {
mycolname <- colnames(mydata)[mycolumn]
coltograph <- mydata[mycolumn]
ggplot(mydata, aes(x = mydata[, mycolumn])) +
geom_histogram(binwidth = 0.1) +
xlab(mycolname) -> plN
if (printit == T) {
print(plN)
}
return(plN)
}


Calling the function as such once looks like:

myhist_function(mydata = cdf, mycolumn = 2, printit = T)


Then I can create an empty list, and loop over whatever columns I desire for any given dataset. And populate the list with ggplot2 graph objects.

mygraphs <- list()

columns_toplot <- names(cdf)
for (i in seq_along(columns_toplot)) {
mygraphs[[i]] <-
myhist_function(mydata = cdf, mycolumn = i, printit = F)
}
names(mygraphs) <- columns_toplot


You can visualize any graph you wish by mygraphs[[i]], but you can also use the ggarrange() function from the ggpubr package to do things like this:

library(ggpubr)
ggarrange(mygraphs[[1]],
mygraphs[[2]],
mygraphs[[3]],
mygraphs[[4]],
ncol = 2,
nrow = 2)


We can store the output in a list

plN <- vector('list', ncol(cdf))
for(i in seq_along(cdf)) {
plN[[i]] <- ggplot(cdf, aes(x= !! rlang::sym(names(cdf)[i]) +
geom_histogram(binwidth = 0.01)

}

plN[[1]]


Answered by akrun on November 15, 2021

## Related Questions

### The Runner of type Shell don’t work: Job failed (system failure): preparing environment:

2  Asked on November 29, 2020 by arthu-vinicius-demetrio-santia

### Webpack: how to copy html files into build folder?

2  Asked on November 29, 2020 by wai-yan-hein

### Save string to file without converting newlines using Python

1  Asked on November 29, 2020 by kleiton-kurti

### Getting typeError: (options.astTransformers || []).map is not a function while running the test suite in Angular

2  Asked on November 28, 2020 by soul-true

### How to make a Column data clickable one in React table?

1  Asked on November 28, 2020 by thomas-martin

### ngx-mask Do not allow negative value for the currency input

2  Asked on November 28, 2020 by ashot-aleqsanyan

### Unity: Add extra time to slider

0  Asked on November 28, 2020 by david-wohlferd

### Python cannot access list

3  Asked on November 28, 2020 by forge-mods

### Spring Boot JPA Hibernate – Storing Date with millisecond precision

3  Asked on November 28, 2020

### How to specify default values for each issue type for custom field on a single project?

0  Asked on November 28, 2020 by ye-k

### Get property name and value only if there is a value

1  Asked on November 28, 2020 by jimenemex

### Count percentage of upper case words

1  Asked on November 28, 2020 by user13623188

### Single tap doesn’t work in combination with double tap

1  Asked on November 28, 2020 by niko-gamulin

### numpy create array of the max of consecutive pairs in another array

7  Asked on November 27, 2020 by galsuchetzky

### How to fetch initial data using provider in flutter effectievly

4  Asked on November 27, 2020 by jithin

### Creating a unique short ID from string value

2  Asked on November 27, 2020 by datta

### How can I send a message to a specific channel?

1  Asked on November 27, 2020 by tuna-ariyurek

### Function specialization with default arguments can’t be called because too few arguments

1  Asked on November 27, 2020 by hellow