Most common functions appearing in R scripts of David Robinson for #TidyTuesday using {funspotr}
#TidyTuesday
Author
Aditya Dahiya
Published
July 10, 2024
How I made this graphic?
Loading required libraries, data import & creating custom functions
Code
# Data Import and Wrangling Toolslibrary(tidyverse) # All things tidy# Final plot toolslibrary(scales) # Nice Scales for ggplot2library(fontawesome) # Icons display in ggplot2library(ggtext) # Markdown text support for ggplot2library(showtext) # Display fonts in ggplot2library(colorspace) # Lighten and Darken colourslibrary(patchwork) # Combining plotslibrary(igraph) # Underlying node edges librarylibrary(ggraph) # Grpahics and ggplot2 with igraphlibrary(tidygraph) # to manuipulate graph objectslibrary(packcircles) # Circles graph# Load datadrob_funs <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-07-09/drob_funs.csv')
# Cleaned data on top 100 functions (only to display top 100)df0 <- drob_funs |>filter(!in_multiple_pkgs) |>count(pkgs, funs, sort = T) |># mutate(# pkgs = case_when(# pkgs == "ggplot" ~ "{ggplot}",# pkgs == "glue" ~ "{glue}",# pkgs == "tune" ~ "{tune}",# .default = name# )# )arrange(pkgs, funs) |>mutate(pkgs =paste0("{", pkgs, "}") )df1 <- df0 |>slice_max(order_by = n, n =100)library(treemapify)ggplot(data = df1,mapping =aes(area = n,fill = pkgs,subgroup = pkgs )) +geom_treemap() +geom_treemap_text(aes(label = funs ) ) +geom_treemap_subgroup_border()# A hierarchical circular packing diagram# Make an edges dataframe from outr data df1edges1 <- df1 |>filter(pkgs!="{(unknown)}") |>rename(from = pkgs,to = funs ) |>select(-n) |>arrange(from)# Make a vertices dataframevertices1 <- df1 |>filter(pkgs!="{(unknown)}") |>select(name = funs, n ) |># add rows for package sizes also by functionsbind_rows( df1 |>group_by(pkgs) |>summarize(n =sum(n)) |>rename(name = pkgs) )edges1vertices1plotdf <-graph_from_data_frame(d = edges1,vertices = vertices1,directed =TRUE)as_tbl_graph(nodes = vertices1,edges = edges1)ggraph( plotdf, layout ="circlepack",weight = n ) +geom_node_circle(mapping =aes(fill = depth ) ) +geom_node_text(mapping =aes(label = name,size = n ) ) +coord_equal() +theme_void() +theme(legend.position ="none" )flare$edges |>as_tibble()flare$vertices |>as_tibble()
Code
df1 <- drob_funs |>filter(!in_multiple_pkgs) |>count(pkgs, funs, sort = T) |>arrange(pkgs, funs) |>slice_max(order_by = n, n =100) |>mutate(fun_labs =paste0(funs, "<br>{", pkgs, "}"),id =row_number() )library(packcircles)# Create the layout using circleProgressiveLayout()# This function returns a dataframe with a row for each bubble.# It includes the center coordinates (x and y) and the radius, which is proportional to the value.top_pkgs <- df1 |>count(pkgs, sort = T) |>slice_max(order_by = n, n =4) |>pull(pkgs)packing1 <-circleProgressiveLayout( df1$n,sizetype ="area")# A tibble of centres of the circles and our cleaned dataplotdf1 <-bind_cols( df1, packing1)# A tibble of the points on the circumference of the circlesplotdf_circle <-circleLayoutVertices( packing1,npoints =100 ) |>as_tibble() |># Adding the 4 main packages for fillleft_join( plotdf1 |>select(id, pkgs) ) |>mutate(pkgs =if_else( pkgs %in% top_pkgs, pkgs,"Others" ),pkgs =fct( pkgs, levels =c( top_pkgs, "Others" ) ) )
Visualization Parameters
Code
# Font for titlesfont_add_google("Nova Mono",family ="title_font") # Font for the captionfont_add_google("Saira Extra Condensed",family ="caption_font") # Font for plot textfont_add_google("Wellfleet",family ="body_font") showtext_auto()# Credits for coffeee palettemypal <- paletteer::paletteer_d("palettesForR::Named")bg_col <-"white"text_col <-"grey10"text_hil <-"#004C4CFF"bts <-80# Caption stuff for the plotsysfonts::font_add(family ="Font Awesome 6 Brands",regular = here::here("docs", "Font Awesome 6 Brands-Regular-400.otf"))github <-""github_username <-"aditya-dahiya"xtwitter <-""xtwitter_username <-"@adityadahiyaias"social_caption_1 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{github};</span> <span style='color: {text_hil}'>{github_username} </span>")social_caption_2 <- glue::glue("<span style='font-family:\"Font Awesome 6 Brands\";'>{xtwitter};</span> <span style='color: {text_hil}'>{xtwitter_username}</span>")
Annotation Text for the Plot
Code
plot_title <-"David Robinson's #TidyTuesday functions"plot_subtitle <-str_wrap("Top 100 functions used in the R scripts. Area of circle denotes how frequently a function appears.", 50)str_view(plot_subtitle)plot_caption <-paste0("**Data:** #TidyTuesday, David Robinson & {funspotr}", " | **Code:** ", social_caption_1, " | **Graphics:** ", social_caption_2 )rm(github, github_username, xtwitter, xtwitter_username, social_caption_1, social_caption_2)