Created
September 25, 2017 08:21
-
-
Save aravindhebbali/2d0d5d5b60d0ef0f4d1b227c8fb0335d to your computer and use it in GitHub Desktop.
ggplot2: Geoms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install | |
install.packages('ggplot2') | |
install.packages('dplyr') | |
install.packages('readr') | |
install.packages('tibble') | |
# library | |
library(ggplot2) | |
library(dplyr) | |
libary(readr) | |
library(tibble) | |
# read data | |
ecom <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/web.csv') | |
ecom | |
# point | |
ggplot(ecom, aes(x = n_pages, y = duration)) + | |
geom_point() | |
# regression line | |
ggplot(mtcars, aes(x = wt, y = mpg)) + | |
geom_point() + | |
geom_abline(intercept = 37.285, slope = -5.344) | |
ggplot(mtcars, aes(x = wt, y = mpg)) + | |
geom_smooth(method = 'lm', se = TRUE) | |
ggplot(mtcars, aes(x = wt, y = mpg)) + | |
geom_smooth(method = 'loess', se = FALSE) | |
# horizontal/vertical lines | |
ggplot(mtcars, aes(x = wt, y = mpg)) + | |
geom_point() + | |
geom_hline(yintercept = 30) | |
ggplot(mtcars, aes(x = wt, y = mpg)) + | |
geom_point() + | |
geom_vline(xintercept = 5) | |
# bar plot | |
# frequency | |
ggplot(ecom, aes(x = factor(device))) + | |
geom_bar() | |
# weight | |
ggplot(ecom, aes(x = factor(device))) + | |
geom_bar(aes(weight = order_value)) | |
# stacked bar plot | |
ggplot(ecom, aes(x = factor(device))) + | |
geom_bar(aes(fill = purchase)) | |
# horizontal bar plot | |
ggplot(ecom, aes(x = factor(device))) + | |
geom_bar(aes(fill = purchase)) + | |
coord_flip() | |
# columns | |
device <- c('laptop', 'mobile', 'tablet') | |
visits <- c(30000, 12000, 5000) | |
traffic <- tibble::tibble(device, visits) | |
ggplot(traffic, aes(x = device, y = visits)) + | |
geom_col(fill = 'blue') | |
# box plot | |
ggplot(ecom, aes(x = factor(device), y = n_pages)) + | |
geom_boxplot() | |
# histogram | |
ggplot(ecom, aes(x = duration)) + | |
geom_histogram() | |
# bins | |
ggplot(ecom, aes(x = duration)) + | |
geom_histogram(bins = 5) | |
# data | |
gdp <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/gdp.csv') | |
gdp | |
# line chart | |
ggplot(gdp, aes(year, india)) + | |
geom_line() | |
# line color & type | |
ggplot(gdp, aes(year, india)) + | |
geom_line(color = 'blue', linetype = 'dashed') | |
# jitter | |
ggplot(ecom, aes(x = factor(device), y = duration)) + | |
geom_jitter() | |
# jitter width and height | |
ggplot(ecom, aes(x = factor(device), y = duration)) + | |
geom_jitter(width = 0.25, height = 0.5) | |
# label | |
ggplot(mtcars, aes(disp, mpg, label = rownames(mtcars))) + | |
geom_label() | |
# text | |
ggplot(mtcars, aes(disp, mpg, label = rownames(mtcars))) + | |
geom_text(check_overlap = TRUE, size = 2) | |
ggplot(mtcars, aes(x = disp, y = mpg, label = rownames(mtcars))) + | |
geom_point() + | |
geom_text(aes(color = cyl), hjust = 0, nudge_x = 0.05, | |
size = 2, angle = 45) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment