Adjust text on plot

I create this plot using ggplot2

I want the text next to the left vertical red line to show the date in the same order as the text in the red vertical line, that is, 03/2020. The desired output should be:

Here is the code:

library(ggplot2)

ggplot(data = ddf_long, aes(x = date, y = value)) +
  # vertical lines first (so they appear behind the data line)
  geom_vline(xintercept = vertical_dates, linetype = "dashed", color = "red", alpha = 0.7, size = 1.5) +
  # shaded area between vertical lines
  geom_rect(aes(xmin = vertical_dates[1], xmax = vertical_dates[2],
                ymin = -Inf, ymax = Inf), fill = "lightgrey", alpha = 0.05) +
  geom_line(color = "black", size = 1) +
  theme_minimal() +
  theme(plot.caption = element_text(hjust = 0)) +
  labs(x = "Date",
       y = "Weighted average radiance") +
  theme_minimal(base_size = 15) +
  theme(
    panel.grid = element_blank(),
    axis.line = element_line(color = "black", size = 0.7),
    panel.border = element_blank(),
    axis.ticks = element_line(color = "black")
  ) +
  # rotated text labels
  annotate("text", x = vertical_dates[1], y = Inf, label = "03/2020", vjust = -1, hjust = 2, angle = 90) +
  annotate("text", x = vertical_dates[2], y = Inf, label = "05/2021", vjust = -1, hjust = -1, angle = -90)

If I understand correctly, you want the label for 03/2020 to read as if you turned the graph 90 degrees to the left, to match the label for 05/2021.

You can adjust the settings this way:

annotate("text", x = vertical_dates[1], y = Inf, label = "03/2020", vjust = 1, hjust = -1, angle = -90) +

If this isn't what you wanted, please provide some clarification and also a sample data set that we can graph.

Wow, that was so obvious! I was changing just the angle and the text was disappearing.

1 Like

This topic was automatically closed 7 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.