View viz_facet.R
# 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) |
View viz_annotations.R
# 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) |
View viz_legend.R
# 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 |
View vis_hist.R
# 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 |
View viz_box.R
# basic plot | |
boxplot(mtcars$mpg) | |
# horizontal box plot | |
boxplot(mtcars$mpg, horizontal = TRUE) | |
# color | |
boxplot(mtcars$mpg, col = 'blue') | |
# border color |
View viz_bar.R
# 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 |
View viz_line.R
# data | |
head(AirPassengers) | |
# plot | |
data <- head(AirPassengers) | |
plot(data, type = 'l') | |
plot(data, type = 'b') | |
plot(data, type = 'o') | |
plot(data, type = 'c') |
View viz_scatter.R
# 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) |
View viz_title_axis_labels.R
# 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 |
View viz_intro.R
# help | |
help(plot) | |
help(mtcars) | |
# mtcars | |
head(mtcars) | |
# variable info | |
str(mtcars) |
NewerOlder