ham-radio-licenses/bin/data-plots.R

312 lines
10 KiB
R
Raw Normal View History

2024-01-09 08:40:06 -06:00
library(ggplot2)
library(tidyr)
library(dplyr)
library(forcats)
library(ggthemes)
library(plotly)
library(htmlwidgets)
library(lubridate)
2024-01-22 22:31:45 -06:00
library(ggrepel)
2024-01-09 08:40:06 -06:00
2024-01-22 22:31:45 -06:00
#### Total/State/Class ####
# Read in total/state/class data
2024-01-09 19:49:07 -06:00
d_raw <- read.csv("data/us ham radio licenses over time.csv")
2024-01-09 08:40:06 -06:00
# What dates do we have?
unique(d_raw$Date)
# Clean up here if needed
d <- d_raw %>%
mutate(Date = as.Date(Date)) #%>%
2024-01-09 19:49:07 -06:00
# Drop Tech alone and leave Tech and Tech Plus
#select(-c(Tech, Tech.Plus))
2024-01-09 19:49:07 -06:00
2024-01-09 08:40:06 -06:00
d_total <- d %>% filter(State.Territory == "TOTAL")
d_total_long <- d_total %>%
pivot_longer(cols = !c("Date", "State.Territory", "source_name", "source_detail"),
2024-01-09 08:40:06 -06:00
names_to = "lclass",
values_to = "count") %>%
# only keep rows with data so plots look right
filter(!is.na(count))
2024-01-12 10:02:56 -06:00
d_state_total_long <- d %>% filter(State.Territory != "TOTAL") %>%
select(Date, State.Territory, Total, source_name, source_detail) %>%
filter(!is.na(Total))
2024-01-22 22:31:45 -06:00
#### City ####
city_raw <- read.csv("data/us cities ham radio licenses over time.csv")
city <- city_raw %>% mutate(Date = as.Date(Date),
city_label = paste0(City, ", ", State))
2024-09-21 19:18:31 -05:00
#### License Actions ####
ae7q_actions <- read.csv("data/ae7q-actions-scraped.csv") %>%
mutate(date = as.Date(date)) %>%
filter(!is.na(action))
2024-01-09 08:40:06 -06:00
#### Plots ####
##### Total over time, y = 0 #####
2024-01-09 08:40:06 -06:00
ggplot(data = d_total,
aes(x = Date,
2024-01-09 19:49:07 -06:00
y = Total,
color = source_name)) +
2024-01-09 08:40:06 -06:00
geom_line() +
geom_point(size = 0.3) +
2024-01-09 19:49:07 -06:00
scale_x_date(date_breaks = "10 years",
date_minor_breaks = "1 year",
2024-01-09 08:40:06 -06:00
date_labels = "%Y") +
scale_y_continuous(labels = scales::comma,
limits = c(0, NA)) +
2024-01-09 08:40:06 -06:00
theme_bw() +
labs(title = "US Amateur Licenses",
2024-01-09 19:49:07 -06:00
caption = "w1cdn.net",
color = "Source") +
theme(legend.position="bottom")
ggplotly()
ggsave("plots/total-over-time-y.png", width = 6, height = 4)
2024-01-09 08:40:06 -06:00
#p <- ggplotly()
#htmlwidgets::saveWidget(as_widget(p), selfcontained = TRUE, file = "plots/total-over-time-y.html")
##### Total over time, since 2000 #####
ggplot(data = d_total %>% filter(Date >= as.Date("2000-01-01")),
2024-01-09 08:40:06 -06:00
aes(x = Date,
2024-01-09 19:49:07 -06:00
y = Total,
color = source_name)) +
2024-01-09 08:40:06 -06:00
geom_line() +
geom_point(size = 0.3) +
2024-01-10 22:11:23 -06:00
scale_x_date(date_breaks = "2 years",
date_minor_breaks = "3 months",
2024-01-09 08:40:06 -06:00
date_labels = "%Y") +
scale_y_continuous(labels = scales::comma) +
2024-01-09 08:40:06 -06:00
theme_bw() +
labs(title = "US Amateur Licenses since 2000",
2024-01-09 19:49:07 -06:00
caption = "w1cdn.net",
color = "Source") +
theme(legend.position="bottom")
2024-01-10 22:11:23 -06:00
ggsave("plots/total-over-time-2000.png", width = 6, height = 4)
2024-01-09 08:40:06 -06:00
##### Total over time, 2 years #####
ggplot(data = d_total %>% filter(Date >= Sys.Date() - years(2)),
aes(x = Date,
y = Total,
color = source_name)) +
geom_line() +
geom_point(size = 0.3) +
scale_x_date(date_breaks = "3 months",
date_minor_breaks = "1 month",
date_labels = "%Y-%m") +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title = paste0("US Amateur Licenses since ", Sys.Date() - years(2)),
caption = "w1cdn.net",
color = "Source") +
theme(legend.position="bottom")
ggsave("plots/total-over-time-2-years.png", width = 6, height = 4)
2024-03-21 09:44:42 -05:00
##### ARRL Total over time, 2 months #####
ggplot(data = d_total %>% filter(Date >= Sys.Date() - months(2),
source_name == "ARRL FCC License Counts"),
aes(x = Date,
y = Total,
color = source_name)) +
geom_line() +
geom_point(size = 0.3) +
scale_x_date(date_breaks = "7 days",
date_minor_breaks = "1 days",
2024-04-07 10:35:13 -05:00
date_labels = "%m-%d") +
2024-03-21 09:44:42 -05:00
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title = paste0("US Amateur Licenses since ", Sys.Date() - months(2)),
caption = "w1cdn.net",
color = "Source") +
theme(legend.position="bottom")
ggsave("plots/total-over-time-2-months.png", width = 6, height = 4)
2024-01-09 08:40:06 -06:00
##### By license class #####
ggplot(data = d_total_long %>% filter(lclass != "Total"),
aes(x = Date,
y = count,
color = fct_reorder2(lclass, Date, count),
linetype = source_name)) +
2024-01-09 08:40:06 -06:00
geom_line() +
geom_point(size = 0.3) +
2024-01-09 19:49:07 -06:00
scale_x_date(date_breaks = "10 years",
date_minor_breaks = "1 year",
2024-01-09 08:40:06 -06:00
date_labels = "%Y") +
scale_y_continuous(labels = scales::comma) +
scale_color_colorblind() +
theme_bw() +
labs(title = "US Amateur Licenses by Class",
y = "Count",
color = "Class",
caption = "w1cdn.net") +
theme(legend.position="bottom")
ggsave("plots/class-over-time.png", width = 12, height = 8)
2024-01-09 08:40:06 -06:00
##### By license class, 2 years #####
ggplot(data = d_total_long %>% filter(lclass != "Total",
Date >= Sys.Date() - years(2)),
aes(x = Date,
y = count,
color = fct_reorder2(lclass, Date, count),
linetype = source_name)) +
geom_line() +
geom_point(size = 0.3) +
2024-01-17 10:21:14 -06:00
scale_x_date(date_breaks = "3 months",
date_minor_breaks = "1 month",
date_labels = "%Y-%m") +
scale_y_continuous(labels = scales::comma) +
scale_color_colorblind() +
theme_bw() +
labs(title = paste0("US Amateur Licenses by Class since ", Sys.Date() - years(2)),
y = "Count",
color = "Class",
caption = "w1cdn.net") +
theme(legend.position="bottom")
ggsave("plots/class-over-time-2-years.png", width = 6, height = 4)
##### By license class, facet #####
ggplot(data = d_total_long %>% filter(lclass != "Total"),
aes(x = Date,
y = count,
color = source_name)) +
geom_line() +
geom_point(size = 0.3) +
facet_wrap(~lclass) +
scale_x_date(date_breaks = "10 years",
date_minor_breaks = "1 year",
date_labels = "%Y") +
scale_y_continuous(labels = scales::comma) +
scale_color_colorblind() +
theme_bw() +
labs(title = "US Amateur Licenses by Class",
y = "Count",
color = "Class",
caption = "w1cdn.net") +
theme(legend.position="bottom")
ggsave("plots/class-over-time-facet.png", width = 15, height = 9)
2024-01-09 08:40:06 -06:00
##### By license class, stacked #####
# ggplot(data = d_total_long %>% filter(lclass != "Total"),
# aes(x = Date,
# y = count,
# fill = fct_reorder2(lclass, Date, count))) +
# geom_area() +
# scale_fill_colorblind() +
# scale_x_date(date_breaks = "10 years",
# date_minor_breaks = "1 year",
# date_labels = "%Y") +
# scale_y_continuous(labels = scales::comma) +
# theme_bw() +
# labs(title = "US Amateur Licenses by Class",
# fill = "Class",
# caption = "w1cdn.net") +
# theme(legend.position="bottom")
#ggsave("plots/class-over-time-stacked.png", width = 6, height = 4)
2024-01-09 08:40:06 -06:00
2024-01-12 10:02:56 -06:00
##### By state #####
ggplot(data = d_state_total_long,
aes(x = Date,
2024-04-21 16:13:03 -05:00
y = Total,
color = source_name)) +
2024-01-12 10:02:56 -06:00
geom_line() +
geom_point(size = 0.3) +
facet_wrap(~State.Territory,
scales = "free_y") +
scale_x_date(date_breaks = "10 years",
#date_minor_breaks = "1 year",
date_labels = "%Y") +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title = "US Amateur Licenses by State",
y = "Count",
caption = "w1cdn.net") +
theme(legend.position="bottom")
2024-01-09 08:40:06 -06:00
2024-01-12 10:02:56 -06:00
ggsave("plots/states-over-time-freey.png", width = 15, height = 9)
2024-01-22 22:31:45 -06:00
##### By city #####
a <- city %>% group_by(city_label) %>%
mutate(plot_label = if_else(Date == max(Date),
city_label,
2024-05-24 20:07:04 -05:00
NA_character_)) %>%
distinct()
2024-01-22 22:31:45 -06:00
ggplot(data = a,
aes(x = Date,
y = Count,
color = city_label)) +
geom_line() +
geom_point(size = 0.3) +
scale_x_date(date_breaks = "1 years",
date_minor_breaks = "1 month",
date_labels = "%Y",
limits = c(NA_Date_, Sys.Date() + years(4))) +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
geom_label_repel(aes(label = plot_label),
nudge_x = 1,
na.rm = TRUE,
segment.colour = NA) +
labs(title = "US Amateur Licenses by City",
y = "Count",
caption = "w1cdn.net") +
theme(legend.position="bottom") +
guides(color = "none")
ggsave("plots/cities-over-time.png", width = 15, height = 9)
##### By city, free y #####
ggplot(data = city,
aes(x = Date,
y = Count,
2024-04-21 16:13:03 -05:00
group = city_label,
color = source_name)) +
2024-01-22 22:31:45 -06:00
geom_line() +
geom_point(size = 0.3) +
facet_wrap(~city_label,
scales = "free_y"
) +
scale_x_date(date_breaks = "4 years",
date_minor_breaks = "1 years",
date_labels = "%Y") +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title = "US Amateur Licenses by City",
y = "Count",
caption = "w1cdn.net") +
theme(legend.position="bottom")
ggsave("plots/cities-over-time-freey.png", width = 15, height = 9)
2024-09-21 19:18:31 -05:00
##### Actions Over Time #####
ggplot(data = ae7q_actions,
aes(x = date,
y = count,
color = action)) +
geom_line() +
scale_x_date(date_breaks = "1 month",
date_minor_breaks = "1 week",
2024-09-21 19:18:31 -05:00
date_labels = "%Y-%m-%d") +
facet_wrap(~action,
scales = "free_y") +
2024-09-21 19:18:31 -05:00
theme_bw() +
labs(title = "US Amateur License Actions",
y = "Count",
x = "Date",
2024-09-21 19:18:31 -05:00
caption = "w1cdn.net",
color = "Action") +
guides(color = "none")
2024-09-21 19:18:31 -05:00
ggsave("plots/actions-over-time.png", width = 6, height = 4)