ggplot2:将facet / strip文本分成两行 [英] ggplot2: Splitting facet/strip text into two lines
问题描述
考虑以下ggplot2图形,其中包含长方形/条形文字
分成两行。
文本出现在专门讨论构面标题的区域之外。
library(ggplot2)
x < - c(1:3,1:3)
y <-c(3:1,1:3)
grp <-c(0,0,0,1,1,1)$ (p)
strip.elem.y <-qplot(x = x,y = y)+ geom_line()+ facet_wrap grid.ls(getGrob(grob,strip.text.x,
grep = TRUE,global = TRUE))$ name
grob< - geditGrob(grob,strip.elem.y [1 ],
label =第一行和第二行)
grid.draw(grob)
有没有办法增加条形文字区域的高度?
多种方式,但得到粘贴(strwrap(text,width = 40),collapse =\\\
使我得到单行数据,而不是连接整个列表中的每一位文本。
)
我想出了一个最适合我的解决方案。我写了一个如下所示的函数。给定一个数据帧 data
并且列 text
wrapit< - function(text){
wtext< - paste(strwrap(text,width = 40),collapse =\\\
)
return )
}
data $ wrapped_text< - llply(data $ text,wrapit)
data $ wrapped_text< - unlist(data $ wrapped_text)
在我调用这个函数之后,我将 Consider the following ggplot2 graph with long facet/strip text
broken in two lines.
The text goes outside the area devoted to facet titles. Is there a way to increase the height of the strip text area ? I tried this a variety of ways but was frustrated getting the I came up with a solution that worked best for me. I wrote a function like the one below. Given a dataframe After I called this function, I just applied my 这篇关于ggplot2:将facet / strip文本分成两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! wrapped_text
列而不是文本
列。 library(ggplot2)
x <- c(1:3, 1:3)
y <- c(3:1, 1:3)
grp <- c(0, 0, 0, 1, 1, 1)
p <- qplot(x=x, y=y) + geom_line() + facet_wrap(~ grp)
grob <- ggplotGrob(p)
strip.elem.y <- grid.ls(getGrob(grob, "strip.text.x",
grep=TRUE, global=TRUE))$name
grob <- geditGrob(grob, strip.elem.y[1],
label="First line and\n second line" )
grid.draw(grob)
paste(strwrap(text, width=40), collapse=" \n")
to give me results for the single row of data and not concatenate the each bit of text from the entire list. data
with column text
wrapit <- function(text) {
wtext <- paste(strwrap(text,width=40),collapse=" \n ")
return(wtext)
}
data$wrapped_text <- llply(data$text, wrapit)
data$wrapped_text <- unlist(data$wrapped_text)
labeller
function to the wrapped_text
column instead of the text
column.