Home > Mobile >  Produce facet_wrap histograms for all numeric columns with faceting for another column in for loop
Produce facet_wrap histograms for all numeric columns with faceting for another column in for loop

Time:11-13

I would like to produce histograms with density lines for all my numeric columns, and facet them by another column.

Using the iris data set as an example, I would like to produce histograms for Sepal.Length etc, with facets for each of Species.

This is what I have tried:

for (i in colnames(subset(iris, select = -`Species`))) {
plot=  ggplot(iris, aes(x= i)) 
    geom_histogram() 
    geom_density(colour = "blue", size = 1)   
    facet_wrap(~ Species, scales = "free")
  
 print(plot)
}

also tried


for (i in colnames(subset(iris, select = -`Species`))) {
plot=  ggplot(subset(iris, select = -`Species`), aes(x= i)) 
    geom_histogram() 
    geom_density(colour = "blue", size = 1)   
    facet_wrap(~ iris$Species, scales = "free")
  
 print(plot)
}

The error I get is Error in f(): ! StatBin requires a continuous x variable: the x variable is discrete.Perhaps you want stat="count"?

Do I need to put something in the geom_histogram() command?

CodePudding user response:

Are you looking for something like this?

  1. We bring the data in long format, with pivot_longer. Then apply fill= name and facet_wrap like you did:
library(tidyverse)

iris %>% 
  pivot_longer(-Species) %>% 
  ggplot(aes(x = value, fill=name)) 
    geom_histogram()  
    geom_density(colour = "blue", size = 1)  
    facet_wrap(~ Species, scales = "free")

enter image description here

CodePudding user response:

I found a helpful answer to this question. I needed to use aes_string. This is what got the desired effect for my original question:

for (i in colnames(subset(iris, select = -`Species`))) {
plot=  ggplot(subset(iris, select = -`Species`), aes_string(x= i)) 
    geom_histogram(aes(y = ..density..)) 
    geom_density(colour = "blue", size = 1)  
    facet_wrap(~ iris$Species, scales = "free")
  
 print(plot)
}

producing separate plots for each variable.

  • Related