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)产生:
ggplot:

但是,我想在整齐的列中左对齐文本。我本质上是在问如何向text提供xmin。我是否需要对相应缩放x的x变量执行数学运算?还是theme有什么窍门?
发布于 2017-09-17 08:38:06
您可以使用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)

https://stackoverflow.com/questions/46259140
复制相似问题