Dav*_*lin 3 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."),
y = row_number(text) - 1,
x = 1)
ggplot(df, aes(x = x, y = y)) +
geom_text(aes(label = text), nudge_x = nchar(text)/2)
Run Code Online (Sandbox Code Playgroud)
生产:
ggplot:

但是,我想在一个整齐的专栏中左对齐文本.我基本上是问如何提供xmin给text.我是否需要对相应x比例的变量执行数学运算x?还是有诀窍theme?
avi*_*seR 10
你可以使用hjust:
ggplot(df, aes(x = x, y = y)) +
geom_text(aes(label = text), hjust = 0)
Run Code Online (Sandbox Code Playgroud)
您还可以添加一个xlim以将列对齐到绘图的最左侧:
ggplot(df, aes(x = x, y = y)) +
geom_text(aes(label = text), hjust = 0) + xlim(1, 1.5)
Run Code Online (Sandbox Code Playgroud)