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