library(ggplot2)
library(dplyr)
library(barnnamn)
library(babynames)

Swedish babynames

This is a Swedish equivalent to the babynames package. Anything you do with babynames you should be able to do with barnnamn instead. The two datasets have the same columns in the same order, so they should also be easy to combine.

In the following example we annotate each dataset with its country of origin, join the two together, and plot the popularity of the girl's name "Anna" over the period for which both datasets have data:

# annotate each dataset with country of origin
barnnamn$country <- "Sweden"
babynames$country <- "USA"

# start where both datasets have data, end at most recent year
start_year <- max(min(babynames$year), min(barnnamn$year))
end_year <- max(c(babynames$year, barnnamn$year))

# join the datasets, filter, and plot
babynames %>%
  bind_rows(barnnamn) %>%
  filter(year >= start_year, year <= end_year) %>%
  filter(name == "Anna", sex == "F") %>%
  ggplot(aes(x = year, y = prop, linetype = country)) +
  geom_line() +
  theme(legend.justification = c(1, 1),
        legend.position = c(0.95, 0.95),
        legend.background = element_blank(),
        legend.key = element_blank()) +
  labs(title="Girls named Anna")

The Swedish names data comes from the Statistiska centralbyrån (Central Statistical Bureau), which provides data from 1998 to 2022. Scripts to process the downloaded data can be found in the source repository for this package



evoling/barnnamn documentation built on March 8, 2024, 7:36 p.m.