Plot Continuous Predictors using Melt


Import packages

library(plyr)
library(reshape2) # for melt function
library(ggplot2)


Reading and Cleaning Wisconsin Breast Cancer Dataset from UCI Machine Learning Repository

breast_cancer <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data")
colnames(breast_cancer) <- c("Code", "ClumpThickness", "UniformCellSize", "UniformCellShape", "MarginalAdhesion", "EpithelialCellSize", "BareNuclei", "BlandChromatin", "NormalNucleoli", "Mitoses", "Class …
more ...

Scatter plot (ggplot)


Import packages

library(ggplot2)


Scatter plot of miles per gallon, mpg, against weight, wt, by number of forward gears, gear, from mtcars dataset

mtcars$gear <- as.character(mtcars$gear) # convert gear to character 
ggplot(mtcars, aes(x = wt, y = mpg, color = gear)) + geom_point(size = 0.7) + geom_smooth(method = lm, se …
more ...

Bar Plot (ggplot)

Import packages

library(ggplot2)


Bar plot count of forward gears, gear, by transmission status, am from mtcars dataset

class(mtcars$am)
## [1] "numeric"
class(mtcars$gear)
## [1] "numeric"
mtcars$am <- as.character(mtcars$am) # convert from numeric to character 
mtcars$gear <- as.character(mtcars$gear) # convert from numeric to character …
more ...

Box plot (ggplot)

Import packages

library(ggplot2)


Boxplot of miles per gallon, mpg, against number of forward gears, gear from mtcars dataset

mtcars$gear <- as.character(mtcars$gear) # convert gear to character 
ggplot(mtcars, aes(x = gear, y = mpg, color = gear)) + geom_point(size = 0.7) + geom_boxplot() + ggtitle("Gear vs. miles per gallon") + theme_bw …
more ...

Density plot (ggplot)

Import packages

library(ggplot2)


Density plot of sepal length, Sepal.Length, by specie status, Species, from iris dataset

ggplot(iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_density(color = "black", alpha = 0.4) + # specify alpha indicating density plot shading frequency
  ggtitle("Distribution of Sepal Length") + theme_bw() + theme(text = element_text(size = 20 …
more ...

Heat map (ggplot)

Import packages

library(datasets)
library(magrittr)
library(dplyr)
library(data.table)
library(reshape2)
library(tidyr)
library(ggplot2)


Reshaping data

# Convert row names to column
mtcars_sc <- mtcars
mtcars_sc[1:11] <- as.data.frame(sapply(mtcars_sc[1:11], as.numeric))
mtcars_scale <- as.data.frame(scale(mtcars_sc))
mtcars_data <- data.table::setDT(data.frame …
more ...

Histogram (ggplot)

Import packages

library(ggplot2)


Histogram of sepal length, Sepal.Length, by specie status, Species, from iris dataset

Default histogram with overlapping bars

ggplot(iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_histogram(bins = 25, color = "black", alpha = 0.7) + # specify alpha indicating histogram bar shading frequency
  ggtitle("Distribution of Sepal Length …
more ...

Pie chart (ggplot)

Import packages

library(ggplot2)
library(plyr)


Pie chart plot of miles per gallon, mpg, against weight, wt, by number of forward gears, gear, from mtcars dataset

tab <- data.frame(table(mtcars$am))
colnames(tab) <- c("Transmission", "Frequency")
tab$Transmission <- revalue(tab$Transmission, c("1" = "manual", "0" = "automatic"))
ggplot(tab, aes …
more ...

Plotting Geographic Data with Leaflet (Color by Categorical Predictor)


Import packages

library(leaflet) 
library(jsonlite) 
library(tibble) 
library(plyr)
library(dplyr)
library(data.table)
library(datasets) # loading datasets package for mtcars and Iris data
library(webshot)


Scrape latitudes and longitudes for US states (from Michelle Hertzfeld's GitHub)

url <- "http://gist.githubusercontent.com/ajav17/dee0dd44357862c75ee2872038119f17/raw/0109432d22f28fd1a669a3fd113e41c4193dbb5d/USstates_avg_latLong"
statesLocation <- fromJSON …
more ...

Plotting Scatter Plot via Facet Wrap (ggplot)


Import packages

library(ggplot2)


Plotting quantitative predictors against Species

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa …
more ...