Grammilo
Grammilo

Reputation: 1369

How to create n plots all with different colours in R?

So I am trying to plot histograms for all numerical columns of a data frame in R. I am plotting using ggplot2 inside a for loop which works fine. What I want is to have different colours for histograms of different columns.

Here is the reproducible code:

library("ggplot2")
library("MASS")
library("corrplot")

data(Boston)

#Data Reading
Boston <- Boston
names(Boston)
#[1] "crim"    "zn"      "indus"   "chas"    "nox"     "rm"      "age"     "dis"     "rad"     "tax"     "ptratio"
#[12] "black"   "lstat"   "medv"

#Getting idea of the data
summary(Boston)
str(Boston)

#Random sampling
index <- sample(1:nrow(Boston), floor(0.8 * nrow(Boston)), replace = FALSE)
training <- Boston[index,]
testing <- Boston[-index,]

#Exploratory Analysis
col = rainbow(ncol(Boston))

#1) Histogram
for (i in 1:ncol(training)) {
  if(is.numeric(training[, i])){
    print(
      ggplot(data = data.frame(training[, i]), aes(x = data.frame(training[,i]), fill = col[i])) + 
        geom_histogram(bins = 10) + 
        ggtitle(paste0(names(training)[i])) + 
        theme(plot.title = element_text(hjust = 0.5)) + 
        labs(x = ""))
  }
}

I want different colour just to make it visually more appealing.

Upvotes: 2

Views: 727

Answers (2)

Sathish
Sathish

Reputation: 12703

Data:

data(Boston)
set.seed(1L) # set random seed
index <- sample(1:nrow(Boston), floor(0.8 * nrow(Boston)), replace = FALSE)
training <- Boston[index,]

Code:

# 1. individual plots

# select numeric and integer type columns
training <- training[, sapply( training, class) %in% c( "numeric", "integer")]
# define colors for columns
col_train <- setNames( rainbow(ncol(Boston)), nm = colnames(training) )
# get ggplot structure for each column
plot_list <- lapply( names( col_train ), function(x){
  ggplot(data = NULL) + 
    geom_histogram(mapping = aes(x = training[, x, drop = TRUE ]), 
                   fill = col_train[ x ],
                   bins = 10) +
    ggtitle(x) + 
    theme(plot.title = element_text(hjust = 0.5)) + 
    labs(x = "")
})
# assign names
names(plot_list) <- names(col_train)
# print plots
print(plot_list)
# print chas
print(plot_list["chas"])
# print first plot
print(plot_list[[1]])

enter image description here

# 2. plots combined in a single window  

library(reshape2)
training <- melt( training )  # melt training data

library(ggplot2)
ggplot(data = training, aes(value, fill = variable)) + 
  geom_histogram(bins = 10)

enter image description here

ggplot(data = training, aes(value, fill = variable)) + 
  geom_histogram(bins = 10) + 
  facet_wrap(~ variable, scales = "free_x")

enter image description here

Upvotes: 2

conrad-mac
conrad-mac

Reputation: 893

Try moving fill = col[i] from the aes function in the ggplot call to inside the geom_histogram call.

#1) Histogram
for (i in 1:ncol(training)) {
      if(is.numeric(training[, i])){
        print(
          ggplot(data = data.frame(training[, i]), aes(x = 
            data.frame(training[,i]))) + 
            geom_histogram(bins = 10, fill = col[i]) + 
            ggtitle(paste0(names(training)[i])) + 
            theme(plot.title = element_text(hjust = 0.5)) + 
            labs(x = ""))
      }
}

Upvotes: 1

Related Questions