R 使用ggplot2左对齐geom_文本层

R 使用ggplot2左对齐geom_文本层,r,ggplot2,geom-text,R,Ggplot2,Geom Text,ggplot2自动将文本居中放置在geom_text层中。例如: library(ggplot2) library(tidyverse) df <- data_frame(text = c("A short sentence.", "A slightly longer sentence.", "This sentence is the longest of the sentences."),

ggplot2
自动将文本居中放置在
geom_text
层中。例如:

library(ggplot2)
library(tidyverse)
df <- data_frame(text = c("A short sentence.",
                      "A slightly longer sentence.",
                      "This sentence is the longest of the sentences."),
             y = row_number(text) - 1,
             x = 1)

ggplot(df, aes(x = x, y = y)) +
  geom_text(aes(label = text), nudge_x = nchar(text)/2)
库(ggplot2)
图书馆(tidyverse)

df您可以使用
hjust

ggplot(df, aes(x = x, y = y)) +
  geom_text(aes(label = text), hjust = 0)
您还可以添加
xlim
以将列与绘图的最左侧对齐:

ggplot(df, aes(x = x, y = y)) +
  geom_text(aes(label = text), hjust = 0)  + xlim(1, 1.5)

另请参见