Created
July 17, 2019 13:58
-
-
Save jthomasmock/2db9db2c534a48af9e2330758be90b8b to your computer and use it in GitHub Desktop.
ggplot2 code for Andy Kirk's article https://www.visualisingdata.com/2019/07/five-ways-to-present-bar-charts/
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
library(rvest) | |
library(tidyverse) | |
# get the data | |
url <- "https://en.wikipedia.org/wiki/List_of_most-streamed_songs_on_Spotify" | |
df <- url %>% | |
read_html() %>% | |
html_table(fill = TRUE) %>% | |
.[[1]] %>% | |
set_names(nm = c("rank", "song_name", "artist", "album", "streams", "date_published")) %>% | |
mutate(num_rank = parse_number(rank), | |
streams_text = case_when(num_rank == 1 ~ paste(streams, "million"), | |
TRUE ~ streams), | |
streams_comma = streams, | |
streams = parse_number(streams), | |
lab_text = paste(rank, song_name, ", by", artist), | |
) | |
# graph 1 | |
(font_height_bars <- df %>% | |
filter(num_rank <=10) %>% | |
ggplot(aes(x = fct_reorder(lab_text, streams), y = streams)) + | |
geom_col(fill = "#00a86b", width = 0.3) + | |
coord_flip() + | |
theme(text = element_text(family = "Nunito Bold", face = "bold"), | |
axis.text = element_text(face = "bold"), | |
axis.ticks = element_blank(), | |
panel.grid.minor = element_blank(), | |
panel.grid.major.y = element_blank(), | |
panel.grid.major.x = element_line(color = "lightgrey")) + | |
labs(y = "Streams (Millions)", | |
x = "") + | |
scale_y_continuous(limits = c(0,2300), expand = c(0, 0), | |
breaks = seq(0, 2250, 250)) + | |
NULL) | |
ggsave("font_height_bars.png", font_height_bars, dpi = 1000, | |
height = 6, width = 16, units = "in") | |
# graph 2 | |
(invis_gridline <- df %>% | |
filter(num_rank <=10) %>% | |
ggplot(aes(x = fct_reorder(lab_text, streams), y = streams)) + | |
geom_col(fill = "#4AA4DE", width = .9) + | |
geom_hline(data = data.frame(x = seq(0, 2250, 250)), | |
aes(yintercept = x), color = "white", size = 0.5) + | |
coord_flip() + | |
theme_minimal() + | |
theme(text = element_text(family = "Nunito Bold", face = "bold"), | |
axis.text = element_text(face = "bold"), | |
axis.ticks = element_blank(), | |
panel.grid = element_blank()) + | |
labs(y = "Streams (Millions)", | |
x = "") + | |
scale_y_continuous(limits = c(0,2300), expand = c(0, 0), | |
breaks = seq(0, 2250, 250)) + | |
NULL) | |
ggsave("invis_gridline.png", invis_gridline, dpi = 1000, | |
height = 6, width = 16, units = "in") | |
#graph 3 | |
(direct_label <- df %>% | |
filter(num_rank <=10) %>% | |
ggplot(aes(x = fct_reorder(lab_text, streams), y = streams)) + | |
geom_col(fill = "#000034", width = .9) + | |
geom_text(aes(x = fct_reorder(lab_text, streams), y = streams, label = streams_text), | |
color = "white", hjust = 1, position = position_nudge(y = -20)) + | |
coord_flip() + | |
theme_minimal() + | |
theme(text = element_text(family = "Nunito Bold", face = "bold"), | |
axis.text = element_text(face = "bold"), | |
axis.text.x = element_blank(), | |
axis.ticks = element_blank(), | |
panel.grid = element_blank()) + | |
labs(y = "", | |
x = "") + | |
scale_y_continuous(limits = c(0,2300), expand = c(0, 0), | |
breaks = seq(0, 2250, 250)) + | |
NULL) | |
ggsave("direct_label.png", direct_label, dpi = 1000, | |
height = 6, width = 16, units = "in") | |
# graph 4 | |
(label_above <- df %>% | |
filter(num_rank <=10) %>% | |
ggplot(aes(x = fct_reorder(lab_text, streams), y = streams)) + | |
geom_col(fill = "#800000", width = .2) + | |
geom_text(aes(x = fct_reorder(lab_text, streams), y = 0, label = lab_text), | |
color = "black", hjust = 0, position = position_nudge(x = 0.3), | |
fontface = "bold", family = "Nunito Bold") + | |
geom_text(aes(x = fct_reorder(lab_text, streams), y = streams, label = streams_text), | |
color = "#800000", hjust = 1, position = position_nudge(y = -20, x = 0.3), | |
fontface = "bold", family = "Nunito Bold") + | |
coord_flip() + | |
theme_minimal() + | |
theme(text = element_text(family = "Nunito Bold", face = "bold"), | |
axis.text = element_blank(), | |
axis.text.x = element_blank(), | |
axis.ticks = element_blank(), | |
panel.grid = element_blank()) + | |
labs(y = "", | |
x = "") + | |
scale_y_continuous(limits = c(0,2300), expand = c(0, 0), | |
breaks = seq(0, 2250, 250)) + | |
NULL) | |
ggsave("label_above.png", label_above, dpi = 1000, | |
height = 6, width = 12, units = "in") | |
# graph 5 | |
(lollipop_bar <- df %>% | |
filter(num_rank <=10) %>% | |
ggplot(aes(x = fct_reorder(lab_text, streams), y = streams)) + | |
geom_col(fill = "grey", width = .8) + | |
geom_point(shape = 21, fill = "orange", color = "black", size = 20, stroke = 1) + | |
geom_text(aes(x = fct_reorder(lab_text, streams), y = streams, label = streams_comma), | |
color = "black", hjust = 0.5, | |
fontface = "bold") + | |
coord_flip() + | |
theme_minimal() + | |
theme(text = element_text(family = "Nunito Bold", face = "bold"), | |
axis.text = element_text(face = "bold"), | |
axis.text.x = element_blank(), | |
axis.ticks = element_blank(), | |
panel.grid = element_blank()) + | |
labs(y = "", | |
x = "") + | |
scale_y_continuous(limits = c(0,2350), expand = c(0, 0), | |
breaks = seq(0, 2250, 250)) + | |
NULL) | |
ggsave("lollipop_bar.png", lollipop_bar, dpi = 1000, | |
height = 8, width = 12, units = "in") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow!