我正在使用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))
Run Code Online (Sandbox Code Playgroud)
因此,通过使用hjust = 0参数,我可以将facet标签文本作为一个单元进行左对齐.
我想做的是左对齐每个单独的文本行.因此,"标签B"和"非常长的标签"都沿着左侧对齐,而不是相对于彼此居中(对于"标签C"和"短"而言也是如此).这可能在ggplot2中吗?在此先感谢您的帮助.
San*_*att 11
使用网格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"))
Run Code Online (Sandbox Code Playgroud)
或者,还有一些代码行可以使用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)
Run Code Online (Sandbox Code Playgroud)
在有人提供真正的解决方案之前,这里有一个黑客:在标签中添加空格以获得您想要的理由.
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))
Run Code Online (Sandbox Code Playgroud)
可能有更简洁的方法来做到这一点,但我没有找到一种方法在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'))
Run Code Online (Sandbox Code Playgroud)
条带文本在下图中左对齐