2023-08-21 Restart in less than an hour

Author
Published

August 21, 2023

It’s been over a year since I last published an update to this blog and six months since I’ve loaded up the project.

Can I get a post written and published succesfully within an hour? It’s been 10mins so far.

Finally using .by in mutate() and summarise()

I initially hated the choice to add .by to mutate() and summarise() but slowly realised I was projecting how I learned on a completely different tidyverse ecosystem.

It’s actually really beautiful to use .by like this:

library(tidyverse)

gss_party_by_marital <- gss_cat %>% 
  summarise(n_in_subcategory = n(), .by = c(partyid, marital)) %>% 
  mutate(n_in_category = sum(n_in_subcategory), .by = partyid) %>% 
  mutate(partyid = fct_reorder(partyid, n_in_category))

gss_party_by_marital %>% 
  ggplot(aes(x = n_in_subcategory,
             y = partyid,
             fill = marital)) +
  geom_col() 

{highcharter} and factors

I’ve intended to write an article about {highcharter} and how it doesn’t rely on factors for years. In lieu of that, here’s a quick 101.

{highcharter} doesn’t respect the factor ordering:

library(highcharter)

gss_party_by_marital %>% 
  hchart(
    type = "bar",
    hcaes(
      y = n_in_subcategory,
      x = partyid,
      group = marital
    )
  ) %>% 
  hc_plotOptions(series = list(stacking = "normal"))

The issue is that {highcharter} needs explicitly empty levels, which the complete() function provides.

gss_completed_data <- gss_cat %>% 
  summarise(n_in_subcategory = n(), .by = c(partyid, marital)) %>% 
  complete(partyid,
           marital,
           fill = list(n_in_subcategory = 0)) %>% 
  mutate(n_in_category = sum(n_in_subcategory), .by = partyid) %>% 
  mutate(partyid = fct_reorder(partyid, n_in_category)) %>% 
  arrange(desc(n_in_category))


gss_completed_data %>% 
  hchart(
    type = "bar",
    hcaes(
      y = n_in_subcategory,
      x = partyid,
      group = marital
    )
  ) %>% 
  hc_plotOptions(series = list(stacking = "normal"))

And that’s a wrap

In 50mins I updated R, RStudio, my packages and got the blog deploying successfully.

Reuse

Citation

BibTeX citation:
@online{hadley2023,
  author = {Hadley, Charlie},
  title = {2023-08-21 {Restart} in Less Than an Hour},
  date = {2023-08-21},
  url = {https://visibledata.co.uk/posts/2023-08-21_restart},
  langid = {en}
}
For attribution, please cite this work as:
Hadley, Charlie. 2023. “2023-08-21 Restart in Less Than an Hour.” August 21, 2023. https://visibledata.co.uk/posts/2023-08-21_restart.