2

I wish to move the label fully to the right of the plot line. I do not wish to extend the x-axis any further. I just need to create space. How do I do this?


library(tidyverse)
library(ggrepel)

df <- tibble(
  x = as.Date(c("1990-01-01", "2000-01-01")),
  y = c(1, 3)
)

lbls <- filter(df, x == "2000-01-01")


ggplot(df, aes(x = x, y = y)) +
  geom_line() +
  geom_label_repel(data = lbls, label = "Hello there I am a very long label") + 
  theme_minimal() 
ixodid
  • 2,180
  • 1
  • 19
  • 46

1 Answers1

1

Adding hjust and expand seemed to do the trick:

ggplot(df, aes(x = x, y = y)) +
  geom_line() +
  geom_label_repel(
    data = lbls, 
    label = "Hello there I am a very long label", 
    hjust = -0.05
  ) + 
  scale_x_date(
    expand = expand_scale(mult = c(0, 1.5)),
    date_labels = "%Y",
    breaks = seq.Date(min(df$x), max(df$x), "5 years")
  ) +
  theme_minimal() +
  theme(panel.grid.minor.x = element_blank())

The hjust argument may not be the same if you have a different date interval on the x axis. For the expand, the c(0, 1.5) means "don't do any adjustment to the min of the x-axis and extend the max of the x-axis another 1.5 times the current range". For the 10 years between 1990 - 2000, another 15 years is added on.

enter image description here

yake84
  • 3,004
  • 2
  • 19
  • 35
  • Is it possible to do this without the gridlines or date labels showing to the the right of the plot line? – ixodid May 10 '19 at 17:41
  • But the horizontal gridlines are still visible. – ixodid May 13 '19 at 00:59
  • You can choose what lines do/don't appear in the theme `theme(panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank(), panel.grid.minor.x = element_blank())` – yake84 May 13 '19 at 02:08