Skip to content

Instantly share code, notes, and snippets.

@expersso
Last active September 6, 2019 15:32
Show Gist options
  • Save expersso/1e20927e69aae14c1961864051dd24e8 to your computer and use it in GitHub Desktop.
Save expersso/1e20927e69aae14c1961864051dd24e8 to your computer and use it in GitHub Desktop.
Wikipedia trend for Python and R
library(tidyverse)
library(scales)
library(stringr)
library(grid)
library(gridExtra)
library(wikipediatrend)
df <- wp_trend(c("R_(programming_language)", "Python_(programming_language)"),
from = "2007-12-01", to = "2017-01-12")
df_trend <- df %>%
mutate(title = str_replace(title, "_.*", "")) %>%
group_by(date = floor_date(date, "month"), title) %>%
summarise(count = sum(count))
p_trend <- ggplot(df_trend, aes(x = date, y = count / 1000, color = title)) +
geom_point() +
geom_smooth(show.legend = FALSE) +
theme_bw() +
theme(legend.position = c(0, 1), legend.justification = c(0, 1),
legend.background = element_blank()) +
labs(x = NULL, y = NULL, color = NULL,
title = "Wikipedia trend for Python and R",
subtitle = "Total number of monthly page views, in thousands")
df_ratio <- df_trend %>%
spread(title, count) %>%
mutate(ratio = Python / R)
p_ratio <- ggplot(df_ratio, aes(x = date, y = ratio)) +
geom_point(alpha = 0.5) +
geom_smooth() +
theme_bw() +
labs(x = NULL, y = NULL, color = NULL,
subtitle = "Ratio of page views (Python / R)")
grid.arrange(p_trend, p_ratio)
@expersso
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment