【问题标题】:How to wrap label when using ggplot2 in knitr?在knitr中使用ggplot2时如何包装标签?
【发布时间】:2013-01-04 18:22:19
【问题描述】:

我正在使用 ggplot2 生成散点图并使用 knitr 导出为 pdf。有些标签有两个长,我想把它包成两行。

示例代码:

\documentclass{article}

\begin{document}

<<echo=FALSE, message=FALSE, warning=FALSE, comment=NA, results='hide'>>=

library("Hmisc")
library("ggplot2")
library("gridExtra")


x1 <- runif(100)
y1 <- rnorm(100)
x2 <- runif(100)
y2 <- rnorm(100)
test <- data.frame(cbind(x1,y1,x2,y2))
label(test$x1) <- "This is a Variable label for variable x1" 
label(test$y1) <- "This is a Variable label for variable y1" 
label(test$x2) <- "This is variable x2" 
label(test$y2) <- "This is variable y2" 

p1 <- ggplot(data = test, aes(x = x1, y = y1)) + 
    geom_point(size = .5) +
    scale_x_continuous(label(test$x1)) +
    scale_y_continuous(label(test$y1)) +
    geom_smooth()

p2 <- ggplot(data = test, aes(x = x2, y = y2)) + 
    geom_point(size = .5) +
    scale_x_continuous(label(test$x2)) +
    scale_y_continuous(label(test$y2)) +
    geom_smooth()

p3 <- ggplot(data = test, aes(x = x1, y = y2)) + 
    geom_point(size = .5) +
    scale_x_continuous(label(test$x1)) +
    scale_y_continuous(label(test$y2)) +
    geom_smooth()

p4 <- ggplot(data = test, aes(x = x2, y = y1)) + 
    geom_point(size = .5) +
    scale_x_continuous(label(test$x2)) +
    scale_y_continuous(label(test$y1)) +
    geom_smooth()

grid.newpage() 
pushViewport(viewport(width = .8, height = .8, layout = grid.layout(nrow=2, ncol=4)))
print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 3))
print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 4))

@

\end{document}

我只想包装 x1 和 y1 的标签,我该怎么做?非常感谢。

【问题讨论】:

    标签: r ggplot2 knitr


    【解决方案1】:

    要使标签分成两行,请在文本应断开的位置添加\n

    label(test$x1) <- "This is a Variable \nlabel for variable x1" 
    label(test$y1) <- "This is a Variable \nlabel for variable y1"
    

    【讨论】:

    • 我无法在我的任务中编辑标签。我还可以做些什么?谢谢。
    【解决方案2】:

    我创建了一个函数来拆分字符串并重建它。

    wrap <- function(char,n){
              char1 <- word(char, start = 1, end=n)
              char2 <- word(char, start = n+1, end=sapply(strsplit(char, " "), length)) 
              char <- paste(char1,"\n",char2)
            }
    
    if (nchar(label(data$x0))>20) {
      label(test$y1) <- wrap(label(test$x1),3)
      label(test$y1) <- wrap(label(test$y1),4)
    }
    

    【讨论】:

    • 另见strwrap函数
    【解决方案3】:

    定义一个函数mystrwrap 以在给定的width 处换行并换行,这样ggplot 可以将其用作标签。将label() 调用封装在mystrwrap 中。这是修改后的 .Rnw 文件。

    \documentclass{article}
    
    \begin{document}
    
    <<echo=FALSE, message=FALSE, warning=FALSE, comment=NA, results='hide'>>=
    
    library(Hmisc)
    library(ggplot2)
    library(grid)
    library(gridExtra)
    
    
    x1 <- runif(100)
    y1 <- rnorm(100)
    x2 <- runif(100)
    y2 <- rnorm(100)
    test <- data.frame(cbind(x1,y1,x2,y2))
    label(test$x1) <- "This is a Variable label for variable x1" 
    label(test$y1) <- "This is a Variable label for variable y1" 
    label(test$x2) <- "This is variable x2" 
    label(test$y2) <- "This is variable y2" 
    
    mystrwrap <- function(x, width = 25) {
      paste(strwrap(x, width = width), collapse = "\n")
    }
    
    p1 <- ggplot(data = test, aes(x = x1, y = y1)) + 
        geom_point(size = .5) +
        scale_x_continuous(mystrwrap(label(test$x1))) +
        scale_y_continuous(mystrwrap(label(test$y1))) +
        geom_smooth()
    
    p2 <- ggplot(data = test, aes(x = x2, y = y2)) + 
        geom_point(size = .5) +
        scale_x_continuous(mystrwrap(label(test$x2))) +
        scale_y_continuous(mystrwrap(label(test$y2))) +
        geom_smooth()
    
    p3 <- ggplot(data = test, aes(x = x1, y = y2)) + 
        geom_point(size = .5) +
        scale_x_continuous(mystrwrap(label(test$x1))) +
        scale_y_continuous(mystrwrap(label(test$y2))) +
        geom_smooth()
    
    p4 <- ggplot(data = test, aes(x = x2, y = y1)) + 
        geom_point(size = .5) +
        scale_x_continuous(mystrwrap(label(test$x2))) +
        scale_y_continuous(mystrwrap(label(test$y1))) +
        geom_smooth()
    
    grid.newpage() 
    pushViewport(viewport(width = .98, height = .8, layout = grid.layout(nrow=2, ncol=4)))
    print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
    print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
    print(p1,vp = viewport(layout.pos.row = 1, layout.pos.col = 3))
    print(p2,vp = viewport(layout.pos.row = 1, layout.pos.col = 4))
    
    @
    
    
    \end{document}
    

    生成的图形需要对视口进行一些调整。 (或者,只需使用gridExtra::grid.arrange

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多