ggplot2:如何从多行分面标签左对齐文本? [英] ggplot2: How to left-justify text from multi-line facet labels?

查看:573
本文介绍了ggplot2:如何从多行分面标签左对齐文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ $ $ $ $ $ $ $ $ $ $ $ set $ $
df = data.frame(facet_label_text = rep(c(Label A,
Label B\
very long label),
Label C\\\
short,
标签D),
each = 5),
time = rep(c(0,4,8,12,16),times = 4),
value = runif( 20,min = 0,max = 100))

#Plot测试数据
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标签文本作为单元左对齐。





我想要做的是左对齐每一行文本。因此,标签B和非常长的标签都沿着左侧对齐,而不是相对于彼此居中(标签C和短同上)。这可能在ggplot2?感谢您的帮助。

解决方案

使用网格 grid.gedit 功能来编辑条。

 库(ggplot2)#v2.1.0 
库(网格)

#您数据
set.seed(3)
df = data.frame(facet_label_text = rep(c(Label A,
Label B\\\
very long label,
(c(0,4,8,12,16),times = c(0,4,8,12,16)),标签C'nshort,
标签D),
each = 5),
time = rep 4),
value = runif(20,min = 0,max = 100))

#你的情节
p = ggplot(df,aes(x = time,y =值))+
geom_line()+
facet_grid(facet_label_text〜。)+
主题(strip.text.y = element_text(angle = 0,hjust = 0))
p

#获取图中的grobs列表
grid.ls(grid.force())

#看起来我们需要GRID.text grobs 。
#但需要注意:
#有GRID.text grobs,它们是条的子项;
#但还有GRID.text grobs是轴的子项。
#因此,一个gPath应该设置
#以获得GRID.text grobs in the strip

#编辑
grid.gedit(gPath( GRID.stripGrob,GRID.text),
just =left,x = unit(0,npc))

或者,可以使用grob对象(代替上述屏幕上的编辑)添加几行代码:

 #获取ggplot grob 
gp = ggplotGrob(p)
grid.ls(grid.force(gp))

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

#绘制
grid.newpage()
grid.draw(gp)


I'm using facet_grid() to display some data, and I have facet labels that span multiple lines of text (they contain the "\n" character).

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))

So by using the hjust = 0 argument, I can left-align facet label text as a unit.

What I would like to do is left-align each individual line of text. So "Label B" and "very long label" are both aligned along the left side, rather than centered relative to each other (ditto for "Label C" and "short"). Is this possible in ggplot2? Thanks in advance for your help.

解决方案

This is fairly straightforward using grid's grid.gedit function to edit the strips.

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"))

Or, a few more lines of code to work with a grob object (in place of editing on screen as above):

# 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)

这篇关于ggplot2:如何从多行分面标签左对齐文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆