Skip to content

Instantly share code, notes, and snippets.

@z3tt
Last active April 14, 2022 04:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z3tt/a35884da020cbf6788c41a8f16569563 to your computer and use it in GitHub Desktop.
Save z3tt/a35884da020cbf6788c41a8f16569563 to your computer and use it in GitHub Desktop.
Create circular scatter plots of daily temperatures for selected cities
library(tidyverse)
## data:
## https://www.kaggle.com/datasets/sudalairajkumar/daily-temperature-of-major-cities?resource=download
temp <- read_csv(here::here("city_temperature.csv"))
## select a few interesting city around the world
temp_selected <-
temp %>%
#filter(str_detect(City, "New York|London|Helsinki|Shanghai|Rio|Jakarta|Cairo|Canberra|Delhi|Capetown|Hong Kong|Reykjavik")) %>%
filter(AvgTemperature != -99) %>%
mutate(
date = paste0(Year, "-", Month, "-", Day),
date = lubridate::ymd(date),
year = lubridate::year(date),
yday = lubridate::yday(date),
month = lubridate::month(date)
)
## create a range of colors, one for each line of latitude (181 in total)
colors <- prismatic::clr_darken(prismatic::clr_saturate(colorRampPalette(rcartocolor::carto_pal(name = "Earth", n = 7))(180), .15), .2)
## list of cities and their latitudinal position
cities_lat <- tibble(
City = sort(unique(temp_selected$City)),
lat = c(30, -35, -34, 29, 60, 22, -6, 52, 41, 64, -23, 31)
) %>%
mutate(color = lat + 90)
## data for plot: selected and html-formatted cities with data from 2011 to 2020
temp_selected_plot <-
temp_selected %>%
filter(year > 2010) %>%
left_join(cities_lat) %>%
mutate(City = glue::glue("<b style='color:{colors[color]}'>{City}</b>"))
## plot
temp_selected_plot %>%
mutate(City = fct_reorder(City, -lat)) %>%
ggplot(aes(yday, AvgTemperature)) +
geom_point(aes(color = AvgTemperature), size = .3) +
coord_polar(start = 0) + #theta = "y"
facet_wrap(~ City) +
guides(color = guide_colorsteps(barwidth = unit(20, "lines"), barheight = unit(.5, "lines"), show.limits = TRUE)) +
scale_x_continuous(breaks = c(1, 91, 182, 274)) +
#scale_color_viridis_c(option = "rocket", direction = -1, end = .95) +
#rcartocolor::scale_color_carto_c()
scico::scale_color_scico(palette = "vikO", direction = 1, begin = .03, end = .97) +
theme_minimal(base_size = 20) +
theme(panel.grid.minor = element_blank(),
text = element_text(color = "grey20"),
axis.title = element_blank(),
legend.position = "bottom",
strip.text = ggtext::element_markdown())
ggsave(here::here("temp-facet-lat.svg"), width = 16, height = 13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment