Generate Expansion vector to left justify text annotation.

I am trying to expand my scale to allow for left justifying the text annotation via ggrepel.

However, I am having trouble keeping the same serve_YYYY_Q ticks. Ideally I want the padding that is provided by using expand = expansion(add = .6) but really only want the increase to occur to the right of the graph and not the left. Where am I going wrong?

library(tidyverse)
library(ggplot2)

data <- tibble(serv_YYYY_Q=as.yearqtr(c("2019 Q3","2019 Q3","2019 Q4","2019 Q4","2020 Q1","2020 Q1","2020 Q2","2020 Q2")),
               labels = c("Florida","All States","Florida","All States","Florida","All States","Florida","All States"),
               share = c(0, 0, 0.001, 0, 0.001, 0.002, 0.029, 0.03))

# Plot and Format

fig <- ggplot(data,aes(x = serv_YYYY_Q, y = share, color = labels)) +
  geom_line() +
  scale_color_manual(
    values = c("#1192FF","#FFB15C")) +
  geom_text_repel(data=tail(data,2),aes(y = share, label = labels), 
                  size = 2.5, hjust=0,nudge_x=.05, 
                  nudge_y = 0.001,
                  direction = "y", segment.color = "white")+
  labs(title = "Telemedicine as a Percent of Physician Payments", subtitle ="") +
  scale_x_yearqtr(format = "%Y-Q%q",expand = expansion(add = .6)) + 
  scale_y_continuous(labels = function(y) paste0(round(y*100,0),"%"), limits = c(0, 0.15), 
                     breaks = seq(0, 0.15, by = .05), expand = c(.005,0)) +
  coord_cartesian(clip = 'off') +
  my_theme +
  theme(plot.title = element_text(size = 8, hjust= -.2, vjust= 0, color = "#595959", face = "bold"),
        plot.subtitle = element_text(size = 7, hjust= 0, vjust=1, color = "#595959"),plot.margin=unit(c(6, 6, 6, 6), "points"), legend.position = "none")

If I understand your question, you can get that simply with expand = expansion(add = c(0,.6))

As per ?expansion:

Then I don't think you should use expand, which changes the scale. If you just add the text, the plotting area automatically increases to encompass it.

In practice, I think the problem is that geom_text_repel() doesn't really respect the nudge_x argument. You can get something better using the standard geom_text(), but you may need to adjust the nudge_y yourself to make it look nice.

For example:

library(tidyverse)
library(zoo)
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
library(ggrepel)

data <- tibble(serv_YYYY_Q=as.yearqtr(c("2019 Q3","2019 Q3","2019 Q4","2019 Q4","2020 Q1","2020 Q1","2020 Q2","2020 Q2")),
               labels = c("Florida","All States","Florida","All States","Florida","All States","Florida","All States"),
               share = c(0, 0, 0.001, 0, 0.001, 0.002, 0.029, 0.03))

# Plot and Format

ggplot(data,aes(x = serv_YYYY_Q, y = share, color = labels)) +
  geom_line() +
  geom_text(data=tail(data,2),
            aes(y = share, label = labels), 
                  size = 2.5,nudge_x = .05,
            nudge_y = c(-1e-4,1e-3))+
  scale_x_yearqtr(format = "%Y-Q%q") +
  theme(plot.title = element_text(size = 8, hjust= -.2, vjust= 0, color = "#595959", face = "bold"),
        plot.subtitle = element_text(size = 7, hjust= 0, vjust=1, color = "#595959"),
        plot.margin=unit(c(6, 6, 6, 6), "points"),
        legend.position = "none")

image

Created on 2020-12-07 by the reprex package (v0.3.0)

Hi Alexis,

I actually figured it out leveraging some of what you did.

I swapped as.yearqtr for as.factor, and scale_x_yearqtr for scale_x_discrete.

Doing that gave me the expansion I wanted and left justified accordingly.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Hi Alexis,

This is exactly what I want in terms of spacing. However, doing the above gives me 2020-Q3 and 2020-Q4.

I want to keep my scale from 2019 -2020-Q2 but just have an expanded scale so that the annotations can left justify.