1

我正在使用 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 的标签,我该怎么做?非常感谢。

4

3 回答 3

1

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

label(test$x1) <- "This is a Variable \nlabel for variable x1" 
label(test$y1) <- "This is a Variable \nlabel for variable y1"
于 2013-01-04T18:55:13.560 回答
0

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

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)
}
于 2013-01-04T22:07:01.187 回答
0

定义一个函数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在此处输入图像描述

于 2017-05-31T06:25:17.550 回答