How can I rotate labels in ggplot2? [duplicate]

做~自己de王妃 提交于 2021-02-10 05:33:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!