Skip to content

Instantly share code, notes, and snippets.

@rexarski
Created August 4, 2021 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexarski/3279b95292b238c358cb7ef90f355da8 to your computer and use it in GitHub Desktop.
Save rexarski/3279b95292b238c358cb7ef90f355da8 to your computer and use it in GitHub Desktop.
# install.packages("ggalt")
library(ggalt)
library(hrbrthemes)
library(tidyverse)
data <- read_csv("searches.csv") %>%
mutate(keyword = as_factor(keyword)) %>%
group_by(keyword) %>%
mutate(idx = 1:n(),
avg=mean(value)) %>%
ungroup() %>%
select(date, keyword, avg, value, idx)
glimpse(data)
mons <- sprintf("2020-%02s-1", 1:49) %>%
as.Date(format = "%Y-%W-%w") %>%
format("%b") %>%
rle()
mons
month_idx <- cumsum(mons$lengths)-3
month_idx
p <- ggplot(data, aes(idx, value)) +
geom_area(alpha = 0.5, stat = "xspline", aes(fill = avg, color = avg)) +
geom_hline(
data = distinct(data, keyword, avg),
aes(yintercept = 0, color = avg), size = 0.5
) +
scale_x_continuous(
expand = c(0,0.125), limits = c(1, 49),
breaks = month_idx, labels = month.abb
) +
scale_y_continuous(expand = c(0,0), limits = c(0, 105)) +
scale_colour_viridis_c(option = "inferno", direction = -1, begin = 0.1, end = 0.9) +
scale_fill_viridis_c(option = "inferno", direction = -1, begin = 0.1, end = 0.9) +
facet_wrap(~keyword, ncol = 1, strip.position = "left", dir = "h") +
labs(
x = NULL, y = NULL, fill = NULL, color = NULL,
title = "Ask 2020: Global Top Search Words in 2020 According to Google",
subtitle = "Height is search interest in a given topic, ranging from 0 to 100.\nColor is average interest in this time period.",
caption = "Source: Google Trends https://trends.google.com/trends/yis/2020/GLOBAL/\nData: Google News Lab."
) +
# theme_ipsum_es(grid="x", axis = "") + # this line raises some errors
theme_ipsum(grid="x", axis = "") +
# theme(strip.text.y = element_text(angle = 180, hjust = 1, vjust = 0)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1, vjust = 0, size=rel(0.618))) +
theme(panel.spacing.y = unit(1, "lines")) +
theme(axis.text.y = element_blank()) +
theme(legend.position = "none")
# import_econ_sans() # this cannot solve the problem
p
data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment