Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 08:39
Show Gist options
  • Save aravindhebbali/a596a4604b2ebda163313caa272f05cd to your computer and use it in GitHub Desktop.
Save aravindhebbali/a596a4604b2ebda163313caa272f05cd to your computer and use it in GitHub Desktop.
ggplot2: Aesthetics
# install
install.packages('ggplot2')
install.packages('dplyr')
install.packages('tidyr')
install.packages('readr')
# library
library(ggplot2)
library(dplyr)
library(tidyr)
library(readr)
# read data
ecom <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/web.csv')
ecom
# Map Color to Variable
ggplot(mtcars, aes(x = disp, y = mpg, color = factor(cyl))) +
geom_point()
# Map Color to Variable
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(aes(color = factor(cyl)))
# Color: Specify Value
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(color = 'blue')
# Map Shape to Variable
ggplot(mtcars, aes(x = disp, y = mpg, shape = factor(cyl))) +
geom_point()
# Map Shape to Variable
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(aes(shape = factor(cyl)))
# Shape: Specify Value
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(shape = 5)
# Shape & Color
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(shape = 5, color = 'blue')
# Fill
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(shape = 5, fill = 'blue')
# Fill: Specify Value
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(shape = 22, fill = 'blue')
# Geom Border Color
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(shape = 22, color = 'blue')
# Map Size to Variable (Continuous)
ggplot(mtcars, aes(x = disp, y = mpg, size = disp)) +
geom_point()
# Specify Size Value
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(size = 4)
# Map Alpha to Variable
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(aes(alpha = factor(cyl)), color = 'blue')
# data
gdp <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/gdp.csv')
gdp
# Line Color
ggplot(gdp, aes(year, india)) +
geom_line(color = 'blue')
# Line Type
ggplot(gdp, aes(year, india)) +
geom_line(linetype = 2)
# Line Type
ggplot(gdp, aes(year, india)) +
geom_line(linetype = 'dashed')
# Line Width
ggplot(gdp, aes(year, india)) +
geom_line(size = 2)
# data
gdp2 <- gdp %>%
select(year, growth, india, china) %>%
gather(key = country, value = gdp, -year)
gdp2
# Grouped Line Chart
ggplot(gdp2, aes(year, gdp, group = country)) +
geom_line()
# Map Line Color to Country
ggplot(gdp2, aes(year, gdp, group = country)) +
geom_line(aes(color = country))
# Map Line Type to Country
ggplot(gdp2, aes(year, gdp, group = country)) +
geom_line(aes(linetype = country))
# Map Line Width to Country
ggplot(gdp2, aes(year, gdp, group = country)) +
geom_line(aes(size = country))
# Map Bar Color to Purchase
ggplot(ecom, aes(device, fill = purchase)) +
geom_bar()
# Map Histogram Color to Purchase
ggplot(ecom) +
geom_histogram(aes(duration, fill = purchase), bins = 10)
# Map Box Plot Color to Purchase
ggplot(ecom) +
geom_boxplot(aes(device, duration, fill = purchase))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment