library(ggplot2)
data(Marriage, package = "mosaicData")
# plot the distribution of race
ggplot(Marriage, aes(x = race)) +
geom_bar(fill = "cornflowerblue",
color="black") +
labs(x = "Race",
y = "Frequency",
title = "Participants by race")

data(mpg, package = "ggplot2")
# stacked bar chart
ggplot(mpg,
aes(x = class,
fill = drv)) +
geom_bar(position = "fill") +
labs(y = "Proportion",
title = "",
fill = "Drive Type")

data(Salaries, package="carData")
# simple scatterplot
ggplot(Salaries,
aes(x = yrs.service,
y = salary)) +
geom_point()

ggplot(Salaries,
aes(x = yrs.since.phd,
y = salary,
color = rank,
shape = sex,
size = yrs.service)) +
geom_point()

library(gapminder)
data(gapminder, package="gapminder")
# Select US cases
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
plotdata <- filter(gapminder,
country == "United States")
# simple line plot
ggplot(plotdata,
aes(x = year,
y = pop)) +
geom_line()

ggplot(Salaries,
aes(x = rank,
y = salary)) +
geom_boxplot() +
labs(title = "Salary distribution by rank")

ggplot(mpg, aes(x = hwy)) +
geom_histogram(fill = "cornflowerblue",
color = "white") +
facet_wrap(~class, ncol = 1) +
labs(title = "Salary histograms by rank")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(Salaries, aes(x = salary / 1000)) +
geom_histogram(color = "white",
fill = "cornflowerblue") +
facet_grid(sex ~ rank) +
labs(title = "Salary histograms by sex and rank",
x = "Salary ($1000)")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

plotdata <- dplyr::filter(gapminder,
continent == "Americas")
ggplot(plotdata, aes(x=year, y = lifeExp)) +
geom_line(color="grey") +
geom_point(color="blue") +
facet_wrap(~country)

gapminder %>%
filter(continent == "Americas") %>%
ggplot(aes(x=year, y = lifeExp)) +
geom_line(color="grey") +
geom_point(color="blue") +
facet_wrap(~country)
