Skip to content

Instantly share code, notes, and snippets.

View aravindhebbali's full-sized avatar
🏠
Working from home

Aravind Hebbali aravindhebbali

🏠
Working from home
View GitHub Profile
@aravindhebbali
aravindhebbali / sort-best-subset.md
Last active March 8, 2022 06:50
Sort best subset selection by AIC
# load packages
library(olsrr)
#> 
#> Attaching package: 'olsrr'
#> The following object is masked from 'package:datasets':
#> 
#>     rivers

# regression
@aravindhebbali
aravindhebbali / viz_facet.R
Last active September 26, 2017 09:18
Data Visualization: Combining Plots
# Case Study 1: combine 4 plots in 2 rows and 2 columns
# store the current parameter settings in init
init <- par(no.readonly=TRUE)
# specify that 4 graphs to be combined and filled by rows
par(mfrow = c(2, 2))
# specify the graphs to be combined
plot(mtcars$mpg)
plot(mtcars$disp, mtcars$mpg)
@aravindhebbali
aravindhebbali / viz_annotations.R
Last active September 26, 2017 09:05
Data Visualization: Text Annotations
# text inside plot
plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text')
# color
plot(mtcars$disp, mtcars$mpg)
text(x = 340, y = 30, labels = 'Sample Text', col = 'red')
# font
plot(mtcars$disp, mtcars$mpg)
@aravindhebbali
aravindhebbali / viz_legend.R
Last active September 26, 2017 08:47
Data Visualization: Legends
# data
year <- seq(2010, 2014, 1)
india <- c(10.3, 6.6, 5.6, 6.6, 7.2)
china <- c(10.6, 9.5, 7.8, 7.7, 7.3)
russia <- c(4.5, 4.3, 3.5, 1.3, 0.7)
brazil <- c(7.5, 3.9, 1.9, 3.0, 0.1)
s_africa <- c(3.2, 3.2, 2.2, 2.2, 1.5)
gdp <- data.frame(year, india, china, russia, brazil, s_africa, stringsAsFactors = FALSE)
gdp
@aravindhebbali
aravindhebbali / vis_hist.R
Last active September 26, 2017 08:33
Data Visualization: Histogram
# distributions
# normal distribution
hist(rbeta(10000, 5, 5), ann = FALSE, col = 'blue')
# skewed distributions
hist(rbeta(10000, 2, 5), ann = FALSE, col = 'blue')
hist(rbeta(10000, 5, 2), ann = FALSE, col = 'blue')
# histogram
# store the results of hist function
@aravindhebbali
aravindhebbali / viz_box.R
Last active September 26, 2017 08:18
Data Visualization: Box Plots
# basic plot
boxplot(mtcars$mpg)
# horizontal box plot
boxplot(mtcars$mpg, horizontal = TRUE)
# color
boxplot(mtcars$mpg, col = 'blue')
# border color
@aravindhebbali
aravindhebbali / viz_bar.R
Last active September 26, 2017 08:14
Data Visualization: Bar Plots
# using plot function
plot(as.factor(mtcars$cyl))
# using bar plot function
barplot(table(mtcars$cyl))
# tabulated data
cyl_freq <- table(mtcars$cyl)
cyl_freq
@aravindhebbali
aravindhebbali / viz_line.R
Last active September 26, 2017 07:46
Data Visualization: Line Graphs
# data
head(AirPassengers)
# plot
data <- head(AirPassengers)
plot(data, type = 'l')
plot(data, type = 'b')
plot(data, type = 'o')
plot(data, type = 'c')
@aravindhebbali
aravindhebbali / viz_scatter.R
Last active September 26, 2017 07:32
Data Visualization: Scatter Plots
# scatter plot
plot(mtcars$disp, mtcars$mpg)
# add title and axis labels
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
# point shape
plot(mtcars$disp, mtcars$mpg, pch = 6)
@aravindhebbali
aravindhebbali / viz_title_axis_labels.R
Last active September 26, 2017 07:18
Data Visualization: Title & Axis Labels
# title
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon')
# subtitle
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement')
# axis labels