ggplot2:如何从多行方面标签左对齐文本?

时间:2016-01-12 17:03:18

标签: r ggplot2

我使用facet_grid()来显示一些数据,并且我有多行文本的构面标签(它们包含" \ n"字符)。

require(ggplot2)

#Generate example data
set.seed(3)
df = data.frame(facet_label_text = rep(c("Label A",
                                         "Label B\nvery long label",
                                         "Label C\nshort",
                                         "Label D"),
                                       each = 5),
                time = rep(c(0, 4, 8, 12, 16), times = 4),
                value = runif(20, min=0, max=100))

#Plot test data
ggplot(df, aes(x = time, y = value)) +
    geom_line() +
    facet_grid(facet_label_text ~ .) +
    theme(strip.text.y = element_text(angle = 0, hjust = 0))

因此,通过使用hjust = 0参数,我可以将facet标签文本左对齐。

enter image description here

我想做的是左对齐每一行文字。所以"标签B"和#34;很长的标签"两者都是沿着左侧对齐,而不是相对于彼此居中(同样为"标签C""短")。这可能在ggplot2中吗?在此先感谢您的帮助。

3 个答案:

答案 0 :(得分:9)

使用网格的grid.gedit函数编辑条带非常简单。

library(ggplot2)  # v2.1.0
library(grid)

# Your data
set.seed(3)
df = data.frame(facet_label_text = rep(c("Label A",
                                         "Label B\nvery long label",
                                         "Label C\nshort",
                                         "Label D"), 
                                       each = 5),
                time = rep(c(0, 4, 8, 12, 16), times = 4),
                value = runif(20, min=0, max=100))

# Your plot
p = ggplot(df, aes(x = time, y = value)) +
    geom_line() +
    facet_grid(facet_label_text ~ .) +
    theme(strip.text.y = element_text(angle = 0, hjust = 0))
p

# Get a list of grobs in the plot
grid.ls(grid.force())  

# It looks like we need the GRID.text grobs.
# But some care is needed:
# There are GRID.text grobs that are children of the strips;
# but also there are GRID.text grobs that are children of the axes.
# Therefore, a gPath should be set up 
# to get to the GRID.text grobs in the strips

# The edit
grid.gedit(gPath("GRID.stripGrob", "GRID.text"),  
         just = "left", x = unit(0, "npc"))

或者,再使用几行代码来处理grob对象(代替上面的屏幕编辑):

# Get the ggplot grob
gp = ggplotGrob(p)
grid.ls(grid.force(gp))

# Edit the grob
gp = editGrob(grid.force(gp), gPath("GRID.stripGrob", "GRID.text"), grep = TRUE, global = TRUE,
          just = "left", x = unit(0, "npc"))

# Draw it
grid.newpage()
grid.draw(gp)

enter image description here

答案 1 :(得分:8)

可能有一种更简洁的方法可以做到这一点,但我没有找到在ggplot2中执行此操作的方法。 padwrap函数可以更通用,因为它基本上只是你所要求的。为了正确理由,我不得不使用单倍间距字体。

# Wrap text with embedded newlines: space padded and lef justified.
# There may be a cleaner way to do this but this works on the one
# example.  If using for ggplot2 plots, make the font `family`
# a monospaced font (e.g. 'Courier')
padwrap <- function(x) {
    # Operates on one string
    padwrap_str <- function(s) {
        sres    <- strsplit(s, "\n")
        max_len <- max(nchar(sres[[1]]))
        paste( sprintf(paste0('%-', max_len, 's'), sres[[1]]), collapse = "\n" )
    }
    # Applys 'padwrap' to a vector of strings
    unlist(lapply(x, padwrap_str))
}

require(ggplot2)

facet_label_text = rep(c("Label A",
                         "Label B\nvery long label",
                         "Label C\nshort",
                         "Label D"), 5)
new_facet_label_text <- padwrap(facet_label_text)

#Generate example data
set.seed(3)
df = data.frame(facet_label_text = new_facet_label_text,
                time = rep(c(0, 4, 8, 12, 16), times = 4),
                value = runif(20, min=0, max=100))

#Plot test data
ggplot(df, aes(x = time, y = value)) +
    geom_line() +
    facet_grid(facet_label_text ~ .) +
    theme(strip.text.y = element_text(angle = 0, hjust = 0, family = 'Courier'))

条形文字在下图中左对齐

enter image description here

答案 2 :(得分:7)

在有人提供真正的解决方案之前,这是一个黑客攻击:在标签中添加空格以获得所需的理由。

require(ggplot2)

#Generate example data
set.seed(3)
df = data.frame(facet_label_text = rep(c("Label A",
                                         "Label B           \nvery long label",
                                         "Label C\nshort     ",
                                         "Label D"),
                                       each = 5),
                time = rep(c(0, 4, 8, 12, 16), times = 4),
                value = runif(20, min=0, max=100))

#Plot test data
ggplot(df, aes(x = time, y = value)) +
  geom_line() +
  facet_grid(facet_label_text ~ .) +
  theme(strip.text.y = element_text(angle = 0, hjust = 0))

enter image description here