使用ggplot2左对齐geom_text图层

时间:2017-09-16 22:51:30

标签: 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)

产地:

ggplot:

link to ggplot (I'm not allowed to post images yet)

但是,我想在一个整洁的专栏中左对齐文本。我基本上是在询问如何向xmin提供text。我是否需要对相应缩放x的{​​{1}}变量执行数学运算?或者x是否有诀窍?

1 个答案:

答案 0 :(得分:9)

您可以使用(env) paper-street:CoinSlack kyle$ gunicorn -b :8000 run [2017-09-16 17:43:59 -0700] [15402] [INFO] Starting gunicorn 19.7.1 [2017-09-16 17:43:59 -0700] [15402] [INFO] Listening at: http://0.0.0.0:8000 (15402) [2017-09-16 17:43:59 -0700] [15402] [INFO] Using worker: sync [2017-09-16 17:43:59 -0700] [15405] [INFO] Booting worker with pid: 15405 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

hjust

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

xlim

enter image description here