问题
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))
回答1:
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)
Unfortunately, angle is not available in geom_label.
回答2:
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)
来源:https://stackoverflow.com/questions/66101889/how-can-i-rotate-labels-in-ggplot2