How can I rotate labels in ggplot2?

Viewed 786

I have a scatter graph with lables. I want to rotate lables - instead of horizontal position I want to see them vertically. I only saw q-s about rotating axis lables on Stackoverflow.

Sample:

mtdata <- mtcars %>%
  rownames_to_column(var = "name")

ggplot(mtdata, aes(x = mpg, y = wt)) + geom_point() +
  geom_label(data = mtdata %>%
               filter(mpg > 20 & wt >3), aes(label = name))

enter image description here

2 Answers

You can use angle parameter if you are ok using geom_text.

library(dplyr)
library(ggplot2)

ggplot(mtdata, aes(x = mpg, y = wt)) + geom_point() +
  geom_text(data = mtdata %>% filter(mpg > 20 & wt >3), 
            aes(label = name), angle = 90, hjust = -1)

enter image description here

Unfortunately, angle is not available in geom_label.

Use ggtext

library(tidyverse)
library(ggtext)
mtdata <- mtcars %>%
  rownames_to_column(var = "name")

ggplot(mtdata, aes(x = mpg, y = wt)) + geom_point() +
  geom_richtext(data = mtdata %>%
               filter(mpg > 20 & wt >3), aes(label = name), angle = 90)

Created on 2021-02-08 by the reprex package (v0.3.0)

Related