y-axis on the right side

A meditation on axis label placement

Recently I’ve been putting my y-axis labels on the right for some time series.

I think this idea has been rattling around in my head since it was suggested on Twitter by Maarten Lambrechts:

Let’s consider by making some mortgage rate plots using a slide modification to my power blue chart theme.

Data stuff

Let’s set up our libraries and get data, using US Weekly average 30-year fixed mortgage rates from Freddie Mac. We can download the data via a handy csv. We’ll also load in a modified version of the power blue theme, the change is making the axis labels dark gray instead of white.

Per usual, we will made these charts with R.

Data stuff and prep

##############################################################
# load libraries ----
##############################################################
library(tidyverse)
library(lubridate)
library(extrafont)
library(data.table)
# works for windows, not sure about others
extrafont::loadfonts(device="win")

##############################################################
# set up theme ----
# modified http://lenkiefer.com/2020/10/30/powder-blue-charts/
# to have dark axis labels
##############################################################

theme_len <-  function(...){
  
  theme_minimal(...)+
    theme(legend.position="top",
          panel.grid.minor=element_blank(),
          panel.grid.major=element_line(color="white"),
          plot.title=element_text(face="bold",color="gray20"),
          plot.subtitle=element_text(face="italic",color="gray20"),
          plot.caption=element_text(hjust=0,color="gray20"),
          legend.direction="horizontal",
          axis.text=element_text(color="gray20"),
          axis.title=element_text(color="gray20"),
          plot.backgroun=element_rect(fill="lightskyblue1"),
          panel.border=element_rect(fill=NA,color=NA),
          plot.margin=margin(1,1,1,1,"cm"),
          legend.key.width=unit(2,"cm")
    )
  
}

##############################################################
# update default ggplot2 line, rect colors/fill ----
##############################################################
inari  <- "#fe5305"
update_geom_defaults("line",list(colour=inari))
update_geom_defaults("rect",list(fill=inari,colour=inari))


##############################################################
# get data  ----
##############################################################
df <- fread("http://www.freddiemac.com/pmms/docs/PMMS_history.csv")
df$date <- as.Date(df$date,format="%m/%d/%Y")
dd <- format(max(df$date),"%B %d, %Y")

Now let’s consider some plots. First a standard plost with labels on the left axis.

Code for standard plot

g_left <- 
ggplot(data=filter(df,date>="2005-01-01"), aes(x=date,y=pmms30))+
  geom_line(size=2,color="white",alpha=0.75)+
  geom_line(size=4,color="white",alpha=0.25)+
  geom_line(size=5,color="white",alpha=0.1)+
  geom_line(size=1.1)+
  theme_len(base_family="Gill Sans MT",base_size=14,base_line_size=0.65)+
  scale_y_continuous(limits=c(2,7),breaks=seq(0,20,1))+
  geom_point(data=.%>% tail(1),size=4,alpha=0.5,color=inari)+
  geom_hline(data=.%>% tail(1),linetype=2,aes(yintercept=pmms30),alpha=1,color=inari)+
  labs(x="",y="",
       subtitle="U.S. Weekly average 30-year fixed mortgage rate (percent)",
       title="Mortgage Rate Trends",
       caption=paste0("@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey through ",
                      dd))
g_left

Code for plot with labels on right

##############################################################
# line plot  ----
##############################################################
g_right <- 
ggplot(data=filter(df,date>="2005-01-01"), aes(x=date,y=pmms30))+
  geom_line(size=2,color="white",alpha=0.75)+
  geom_line(size=4,color="white",alpha=0.25)+
  geom_line(size=5,color="white",alpha=0.1)+
  geom_line(size=1.1)+
  theme_len(base_family="Gill Sans MT",base_size=14,base_line_size=0.65)+
  scale_y_continuous(position="right",limits=c(2,7),breaks=seq(0,20,1))+
  geom_point(data=.%>% tail(1),size=4,alpha=0.5,color=inari)+
  geom_hline(data=.%>% tail(1),linetype=2,aes(yintercept=pmms30),alpha=1,color=inari)+
  labs(x="",y="",
       subtitle="U.S. Weekly average 30-year fixed mortgage rate (percent)",
       title="Mortgage Rate Trends",
       caption=paste0("@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey through ",
                      dd))
g_right

We could also put the labels on both sides, which I sometimes do, but that is redundant and also shows the same information twice.

labels on both sides

##############################################################
# line plot  ----
##############################################################
g_both <- 
ggplot(data=filter(df,date>="2005-01-01"), aes(x=date,y=pmms30))+
  geom_line(size=2,color="white",alpha=0.75)+
  geom_line(size=4,color="white",alpha=0.25)+
  geom_line(size=5,color="white",alpha=0.1)+
  geom_line(size=1.1)+
  theme_len(base_family="Gill Sans MT",base_size=14,base_line_size=0.65)+
  scale_y_continuous(sec.axis=dup_axis(),limits=c(2,7),breaks=seq(0,20,1))+
  geom_point(data=.%>% tail(1),size=4,alpha=0.5,color=inari)+
  geom_hline(data=.%>% tail(1),linetype=2,aes(yintercept=pmms30),alpha=1,color=inari)+
  labs(x="",y="",
       subtitle="U.S. Weekly average 30-year fixed mortgage rate (percent)",
       title="Mortgage Rate Trends",
       caption=paste0("@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey through ",
                      dd))
g_both

Which do you prefer? Honestly I’ll probably cycle between all three and tend to stick with labels on the left due to inertia. But right now the labels on the right are my favorite.

 Share!