Vis 01
library(datasets)
library(ggplot2)
data(mpg)
mpg <- mpg
head(mpg)
## # A tibble: 6 x 11
## manufacturer model displ year cyl trans drv cty hwy fl
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr>
## 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p
## 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p
## 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p
## 4 audi a4 2.0 2008 4 auto(av) f 21 30 p
## 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p
## 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p
## # ... with 1 more variables: class <chr>
ggplot(mpg, aes(class, fill=trans)) +
geom_bar(position = "stack") +
scale_fill_discrete(name = "Transmission")

Vis 02
ggplot(mpg, aes(x = manufacturer, y = hwy)) +
geom_boxplot() +
coord_flip() +
labs(y ="Highway Fuel Efficiency (miles/gallon)" , x = "Vehicle Manufacturer") +
theme_classic()

Vis 03
library(ggthemes)
data("diamonds")
head(diamonds)
## # A tibble: 6 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
ggplot(diamonds, aes(price, color = cut, fill = cut)) +
geom_density(alpha = 0.2) +
labs(x = "Diamond Price (USD)", y = "Density") +
ggtitle(label = "Diamond Price Density") +
theme_economist()

Vis 04
data(iris)
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
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
geom_smooth(method = lm) +
labs(x = "Iris Sepal Length", y = "Iris Petal Length") +
ggtitle("Relationship between Sepal and Petal Length") +
theme_minimal()

Vis 05
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, fill = Species, color = Species)) +
geom_point() +
geom_smooth(method = lm, se = FALSE) +
labs(x = "Iris Sepal Length", y = "Iris Petal Length") +
ggtitle(label = "Relationship between Sepal and Petal Length", subtitle = "Species Level Comparison") +
theme_tufte() +
theme(legend.position = "bottom")
