DJC
DJC

Reputation: 1611

Left-align and right-justify text using draw_label

When using draw-label, is there anyway to left-align and right-justify text similiar to what ms word does?

enter image description here

Desired end-result style text:

enter image description here

chart <- mtcars %>% 
  ggplot(aes(x = hp, y = cyl)) +
  geom_point()

label <- ggdraw() +
    draw_label(label = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt\nut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco\nlaboris nisi ut aliquip ex ea commodo.",
               size = 8,
               hjust = 0, 
               x = 0.05)

plot_grid(label,
          chart,
          ncol = 1)

Upvotes: 0

Views: 798

Answers (1)

MarBlo
MarBlo

Reputation: 4534

If you want to do this with cowplot 's draw_label you may use rel_heights, see below.

But as @Tjebo pointed out the text chosen is maybe a bit long for a figure caption - see second part.

library(tidyverse)
library(cowplot)

chart <- mtcars %>% 
  ggplot(aes(x = hp, y = cyl)) +
  geom_point()


p1 <- ggdraw() + 
  draw_label(label = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
             sed do eiusmod tempor incididunt\nut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco\nlaboris nisi ut aliquip ex ea commodo.",
             size = 8)

# You can use **rel_heights**
plot_grid(p1, chart, ncol = 1, align = "h", rel_heights = c(1,4))

# Text is a bit long for a figure caption and should somewhere else
# But you could use title, subtitle and caption, which you can move
chart + 
  labs(caption = "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
       title = 'LOREM IPSUM',
       subtitle = '... dolor sit amet, consectetur adipiscing elit') +
  theme(plot.caption = element_text(hjust = 1, face= "italic"), 
        plot.title.position = "plot", 
        plot.caption.position =  "plot")

References

Upvotes: 1

Related Questions