Home > Enterprise >  How can I label only the first quarter of each year in ggplot w. geom_text?
How can I label only the first quarter of each year in ggplot w. geom_text?

Time:11-12

I'm trying to create labels in ggplot with geom_point and geom_text, that labels only the first quarter of each year, and no labels in between. The labelled 'dots' should just give the year.

So 2009 2010 2011 and so on. I'm sure there is a simple way to do this, but I'm stuck!

I should add that I'm looking to do this "programmatically" and not manually, as I will be doing this for multiple data-sets and several years. Appreciate any help!

CODE:

library(tidyverse)
library(pxweb)
library(lubridate)


# Download data from Statistic Sweden

# PXWEB query 
pxweb_query_list <- 
  list("Kon"=c("1 2"),
       "Alder"=c("tot16-64"),
       "Arbetskraftstillh"=c("ALÖS"),
       "ContentsCode"=c("0000062S"),
       "Tid"=c("*"))

# Download data on unemployment and vacancy rates
unemp <- 
  pxweb_get(url = "https://api.scb.se/OV0104/v1/doris/en/ssd/AM/AM0403/AM0403A/NAKUBefolkning2KTD",
            query = pxweb_query_list)

# Convert to data.frame 
unemp <- as.data.frame(unemp, column.name.type = "text", variable.value.type = "text")


# PXWEB query 
pxlist2 <- 
  list("Antanstallda"=c("TOT"),
       "ContentsCode"=c("AM0701F4"),
       "Tid"=c("*"))

vacancy <- 
  pxweb_get(url = "https://api.scb.se/OV0104/v1/doris/en/ssd/AM/AM0701/AM0701D/KVRekVakStor",
            query = pxlist2)

# Convert to data.frame 
vacancy <- as.data.frame(vacancy, column.name.type = "text", variable.value.type = "text")

rm(pxlist2, pxweb_query_list)

# Fix date-format
unemp$quarter <- yq(unemp$quarter)
vacancy$quarter <- yq(vacancy$quarter)

# Join df's
final <- inner_join(unemp, vacancy, by = "quarter")

# clean names
final <- final %>% janitor::clean_names()

# rename etc
final <- final %>% 
  mutate(unemp = percent) %>% 
  mutate(date = quarter) %>% 
  select(date, unemp, vacancy_rate)

# Seperate data in two periods
final <- final %>% 
  mutate(period = 
           case_when(
             date>=ymd('2002-04-01') & date<ymd('2008-01-01') ~  "one",
             date>=ymd('2008-01-01') & date<=ymd('2015-01-01') ~  "two")) 

ggplot(final)  
  aes(x = unemp,
      y = vacancy_rate,
      group = period,
      color = period,
      label = date)  
  geom_point()  
  geom_path()  
  geom_text()  
  facet_wrap(~period)  
  theme_gray()

The result currently looks like this (it's mid-process, I know it looks ugly):

enter image description here

CodePudding user response:

Often the simplest way to do this is just create another label variable in your dataframe, and use ifelse() to set the labels you don't want to an empty string "".

See the example below, where I create date_label that is the year when the month == 1, and any other time the label is "".

final <- final %>% 
  mutate(
    period = case_when(
      date>=ymd('2002-04-01') & date<ymd('2008-01-01') ~  "one",
      date>=ymd('2008-01-01') & date<=ymd('2015-01-01') ~  "two"),
    date_label = ifelse(month(date) == 1, year(date), "")
  ) 

The graph still looks hella ugly, but at least the labels are now right :) enter image description here

  • Related