1

ggplot2を使用して散布図を生成し、knitrを使用してpdfにエクスポートしています。いくつかのラベルは2つの長さで、2行に折り返したいと思います。

サンプルコード:

\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

2行でラベルを作成する\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 に答える