library(ggplot2)
<- readr::read_csv('https://mlorenzen.com/a_csvfile/aapl_amzn_stock_prices.csv')
aapl_amzn_stock_prices
|>
aapl_amzn_stock_prices ggplot(aes(x = date, y = close, color = stock_symbol)) +
geom_line(linewidth = 1) +
labs(
title = "Apple and Amazon Stock Prices",
caption = 'Data: TidyTuesday 2023 - Week 06',
x = element_blank(),
y = "Closing Price (in USD)",
color = 'Company'
+
) theme_minimal(base_size = 14, base_family = "") +
theme(
panel.grid.minor = element_blank()
)
Charts and Code
5 Example Charts with ggplot2
Inspired by this blog post by Albert Rapp.
Line Chart
Bar Chart
library(ggplot2)
library(dplyr)
<- nycflights13::flights |>
flights_count filter(!is.na(dep_time)) |>
count(origin) |>
mutate(origin = case_when(
== "EWR" ~ "Newark Liberty Airport",
origin == "JFK" ~ "John F. Kennedy Airport",
origin == "LGA" ~ "LaGuardia Airport"
origin
))
|>
flights_count ggplot(aes(y = origin, x = n)) +
geom_col(fill = 'firebrick3') +
theme_minimal(base_size = 14, base_family = "") +
theme(
panel.grid.minor = element_blank()
+
) labs(
x = element_blank(),
y = element_blank(),
title = "Number of Flights Leaving NYC in 2013"
)
<- mpg |>
manufacturers ::mutate(manufacturer = stringr::str_to_title(manufacturer)) dplyr
library(ggplot2)
library(dplyr)
library(forcats)
|>
manufacturers mutate(
manufacturer = forcats::fct_infreq(manufacturer) |>
::fct_rev()
forcats|>
) ggplot(aes(y = manufacturer)) +
geom_bar(fill = 'dodgerblue4') +
geom_text(
data = count(manufacturers, manufacturer),
mapping = aes(
x = n, y = manufacturer, label = n
),hjust = 1,
nudge_x = -0.25,
color = 'white',
size = 6
+
) labs(
x = element_blank(),
y = element_blank(),
title = 'Number of cars in the {mpg} dataset'
+
) theme_minimal(
base_size =14,
base_family = ''
+
) theme(
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
plot.title = element_text(
# family = 'Merriweather',
size = rel(1.2)
),plot.title.position = 'plot'
+
) scale_x_continuous(
expand = expansion(mult = c(0, 0.01))
)
library(ggplot2)
library(dplyr)
library(forcats)
library(stringr)
|>
manufacturers mutate(
manufacturer = forcats::fct_infreq(manufacturer) |>
::fct_rev()
forcats|>
) ggplot(aes(y = manufacturer)) +
geom_bar(
just = 1,
fill = 'dodgerblue4',
width = 0.4
+
) geom_text(
data = count(manufacturers, manufacturer),
mapping = aes(
x = n,
y = manufacturer,
label = n
),hjust = 1,
vjust = 0,
nudge_y = 0.1,
color = 'grey30',
fontface = 'bold',
size = 4
+
) geom_text(
data = count(manufacturers, manufacturer),
mapping = aes(
x = 0,
y = manufacturer,
label = stringr::str_to_title(manufacturer)
),hjust = 0,
vjust = 0,
nudge_y = 0.1,
nudge_x = 0.05,
color = 'grey30',
fontface = 'bold',
size = 4
+
) labs(
y = element_blank(),
x = element_blank(),
title = 'Number of cars in the {mpg} dataset'
+
) theme_minimal(
base_size = 14,
base_family = ''
+
) theme(
panel.grid = element_blank(),
plot.title = element_text(
# family = 'Merriweather',
size = rel(1.2)
),plot.title.position = 'plot'
+
) geom_vline(xintercept = 0) +
scale_x_continuous(
breaks = NULL,
expand = expansion(mult = c(0, 0.01))
+
) scale_y_discrete(breaks = NULL)
Histogram
library(ggplot2)
library(dplyr)
<- nycflights13::flights |>
departed_flights filter(!is.na(dep_delay))
|>
departed_flightsggplot(aes(x = dep_delay)) +
geom_histogram(fill = 'dodgerblue4', binwidth = 5) +
coord_cartesian(xlim = c(-20, 300)) +
theme_minimal(base_size = 14, base_family = "") +
theme(
panel.grid.minor = element_blank()
+
) labs(
x = 'Departure Delay (in minutes)',
y = element_blank(),
title = "Number of Delayed Flights Leaving NYC in 2013"
)
Scatterplot + Bubble Chart
library(ggplot2)
library(dplyr)
::gapminder |>
gapminderfilter(year == 2007) |>
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(
aes(color = continent, size = pop),
alpha = 0.8
+
) theme_minimal(base_size = 14, base_family = "") +
theme(
panel.grid.minor = element_blank()
+
) labs(
x = 'GDP per Capita (in USD)',
y = 'Life Expectancy (in years)',
title = "Life Expectancies and GDP per Capita in 2007",
color = 'Continent',
size = 'Population'
+
) guides(size = guide_none())
Heatmap
library(ggplot2)
library(dplyr)
library(lubridate)
<- departed_flights |>
flights_day_counts mutate(
date = lubridate::make_date(year, month, day),
week = lubridate::week(date),
day = lubridate::wday(date, week_start = 1)
|>
) count(week, day) |>
mutate(
day = case_when(
== 1 ~ "Monday",
day == 2 ~ "Tuesday",
day == 3 ~ "Wednesday",
day == 4 ~ "Thursday",
day == 5 ~ "Friday",
day == 6 ~ "Saturday",
day == 7 ~ "Sunday"
day
),day = factor(day, levels = c(
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
)
)
)
|>
flights_day_counts ggplot(aes(x = week, y = day, fill = n)) +
geom_tile(color = 'white') +
coord_equal() +
scale_fill_gradient(low = 'white', high = 'dodgerblue3') +
theme_minimal(base_size = 14, base_family = "") +
theme(
panel.grid.minor = element_blank(),
legend.position = 'top'
+
) labs(
x = 'Week',
y = element_blank(),
title = "Number of Flights Leaving NYC in 2013",
fill = 'Flights'
+
) guides(
fill = guide_colorbar(barwidth = unit(10, 'cm'))
)