为多图面板添加注释后修改 space
Modify space after adding annotate for multiplot panels
我有一个带有注释(在绘图之外)的多绘图面板,我想修改它。
剧情如下:
我怎样才能:
- 将标题(I. 和 II.)移到最左边?
- 更改标题周围的白色 space 数量?
- 更改绘图周围白色的数量 space?
(首选ggplot()
解决方案)
这是获得上图的代码(改编自 OP)(删除了 theme
等不需要的代码):
panelA <- data.frame(
Stage = c("Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling"),
Individual = c ("A", "A", "A","B", "B", "B","C", "C", "C","D", "D", "D"),
Score = c( 1.4, 1.2, NA,0.4, 0.6, 0.5,-0.3, -0.5, -0.4,-1.4, -1.2, NA))
A<-ggplot(panelA, aes(x = Stage, y = Score, color =Individual, group= Individual)) +
geom_point() +
geom_line()+
geom_smooth(method=lm, se=F, fullrange=TRUE)
panelB <- data.frame(
Stage = c("Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling"),
Individual = c ("A", "A", "A","B", "B", "B","C", "C", "C","D", "D", "D"),
Score = c( 1.4, 1.2, 1.3,0.4, 0.6, NA,-0.3, -0.5, NA,-1.4, -1.2, -1.3))
B<-ggplot(panelB, aes(x = Stage, y = Score, color =Individual, group= Individual)) +
geom_point() +
geom_line()+
geom_smooth(method=lm, se=F, fullrange=TRUE)
library(ggplot2)
library(gridExtra)
library(RGraphics)
library(cowplot)
grid.newpage()
# Create layout : nrow = 4, ncol = 2
pushViewport(viewport(layout = grid.layout(4, 2)))
# A helper function to define a region on the layout
define_region <- function(row, col){
viewport(layout.pos.row = row, layout.pos.col = col)
}
#text I want to annotate
t1 <- ggdraw() + draw_label("I. Effects on variance components", fontface='bold')
t2 <- ggdraw() + draw_label("II. Effects on means (mediated via plasticity)", fontface='bold')
# Arrange the plots
print(t1, vp=define_region(1, 1))
print(A, vp = define_region(2, 1))
print(B, vp=define_region(2, 2))
print(t2, vp = define_region(3, 1))
print(A, vp=define_region(4, 1))
print(B, vp = define_region(4, 2))
所以这里将是一个 semi-pure ggplot 解决方案来解决你的问题,不需要这些额外的包,除了 ggplot 已经依赖的包(以及另一个 ggplot 已经依赖的注释包)。
要水平减少两个面板之间的 space,您可以使用小平面而不是 copy-pasting 整个图,包括多余的轴、白色 space 和诸如此类的东西:
AB <- ggplot(mapping = aes(Stage, Score, colour = Individual, group = Individual)) +
geom_point(data = cbind(panelA, panel = "A")) +
geom_point(data = cbind(panelB, panel = "B")) +
geom_line(data = cbind(panelA, panel = "A")) +
geom_line(data = cbind(panelB, panel = "B")) +
facet_wrap(~ panel, ncol = 2)
现在要减少面板内的 space,从点到边缘,您可以调整某些比例的扩展参数。将 0.1 值设置为更小或更大:
AB <- AB + scale_x_discrete(expand = c(0,0.1))
在实际情况下,您可能不需要两次相同的绘图,但是由于您给出了一个垂直绘制相同绘图的示例,因此我将遵循这一思路。所以现在我们结合情节:
top <- AB + ggtitle("I. Effects on variance components")
bottom <- AB + ggtitle("II. Effects on means (mediated via plasticity)")
combined <- rbind(ggplotGrob(top), ggplotGrob(bottom), size = "first")
但是由于我们已经通过 ggplotGrob()
将绘图转换为 gtables,我们现在需要网格语法来绘制绘图:
grid::grid.newpage(); grid::grid.draw(combined)
如下所示:
由于 data.frames 中的 NA
,我收到了一些警告,但除此之外,这通常不应该发生。如果你不喜欢这些条带(面板标题喜欢深灰色框中的东西),你可以简单地调整主题:+ theme(strip.background = element_blank(), strip.text = element_blank())
在你的绘图代码中。
您可以按如下方式添加自定义注释:
combined <- gtable::gtable_add_grob(
combined,
grid::textGrob("Here be some annotation", x = 1, hjust = 1),
t = 16, l = 9 # top/left positions of where to insert this
)
grid::grid.newpage(); grid::grid.draw(combined)
但是请注意,rbind()
将 plotgrobs 组合在一起需要它们具有相同数量的列,因此您不能在一个图中省略 legend/guide 而在另一个图中不省略。不过,您可以在 gtable 中删除其中之一。
我有一个带有注释(在绘图之外)的多绘图面板,我想修改它。
剧情如下:
我怎样才能:
- 将标题(I. 和 II.)移到最左边?
- 更改标题周围的白色 space 数量?
- 更改绘图周围白色的数量 space?
(首选ggplot()
解决方案)
这是获得上图的代码(改编自 OP)(删除了 theme
等不需要的代码):
panelA <- data.frame(
Stage = c("Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling"),
Individual = c ("A", "A", "A","B", "B", "B","C", "C", "C","D", "D", "D"),
Score = c( 1.4, 1.2, NA,0.4, 0.6, 0.5,-0.3, -0.5, -0.4,-1.4, -1.2, NA))
A<-ggplot(panelA, aes(x = Stage, y = Score, color =Individual, group= Individual)) +
geom_point() +
geom_line()+
geom_smooth(method=lm, se=F, fullrange=TRUE)
panelB <- data.frame(
Stage = c("Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling","Juvenile", "Juvenile", "Yearling", "Juvenile", "Juvenile", "Yearling"),
Individual = c ("A", "A", "A","B", "B", "B","C", "C", "C","D", "D", "D"),
Score = c( 1.4, 1.2, 1.3,0.4, 0.6, NA,-0.3, -0.5, NA,-1.4, -1.2, -1.3))
B<-ggplot(panelB, aes(x = Stage, y = Score, color =Individual, group= Individual)) +
geom_point() +
geom_line()+
geom_smooth(method=lm, se=F, fullrange=TRUE)
library(ggplot2)
library(gridExtra)
library(RGraphics)
library(cowplot)
grid.newpage()
# Create layout : nrow = 4, ncol = 2
pushViewport(viewport(layout = grid.layout(4, 2)))
# A helper function to define a region on the layout
define_region <- function(row, col){
viewport(layout.pos.row = row, layout.pos.col = col)
}
#text I want to annotate
t1 <- ggdraw() + draw_label("I. Effects on variance components", fontface='bold')
t2 <- ggdraw() + draw_label("II. Effects on means (mediated via plasticity)", fontface='bold')
# Arrange the plots
print(t1, vp=define_region(1, 1))
print(A, vp = define_region(2, 1))
print(B, vp=define_region(2, 2))
print(t2, vp = define_region(3, 1))
print(A, vp=define_region(4, 1))
print(B, vp = define_region(4, 2))
所以这里将是一个 semi-pure ggplot 解决方案来解决你的问题,不需要这些额外的包,除了 ggplot 已经依赖的包(以及另一个 ggplot 已经依赖的注释包)。
要水平减少两个面板之间的 space,您可以使用小平面而不是 copy-pasting 整个图,包括多余的轴、白色 space 和诸如此类的东西:
AB <- ggplot(mapping = aes(Stage, Score, colour = Individual, group = Individual)) +
geom_point(data = cbind(panelA, panel = "A")) +
geom_point(data = cbind(panelB, panel = "B")) +
geom_line(data = cbind(panelA, panel = "A")) +
geom_line(data = cbind(panelB, panel = "B")) +
facet_wrap(~ panel, ncol = 2)
现在要减少面板内的 space,从点到边缘,您可以调整某些比例的扩展参数。将 0.1 值设置为更小或更大:
AB <- AB + scale_x_discrete(expand = c(0,0.1))
在实际情况下,您可能不需要两次相同的绘图,但是由于您给出了一个垂直绘制相同绘图的示例,因此我将遵循这一思路。所以现在我们结合情节:
top <- AB + ggtitle("I. Effects on variance components")
bottom <- AB + ggtitle("II. Effects on means (mediated via plasticity)")
combined <- rbind(ggplotGrob(top), ggplotGrob(bottom), size = "first")
但是由于我们已经通过 ggplotGrob()
将绘图转换为 gtables,我们现在需要网格语法来绘制绘图:
grid::grid.newpage(); grid::grid.draw(combined)
如下所示:
由于 data.frames 中的 NA
,我收到了一些警告,但除此之外,这通常不应该发生。如果你不喜欢这些条带(面板标题喜欢深灰色框中的东西),你可以简单地调整主题:+ theme(strip.background = element_blank(), strip.text = element_blank())
在你的绘图代码中。
您可以按如下方式添加自定义注释:
combined <- gtable::gtable_add_grob(
combined,
grid::textGrob("Here be some annotation", x = 1, hjust = 1),
t = 16, l = 9 # top/left positions of where to insert this
)
grid::grid.newpage(); grid::grid.draw(combined)
但是请注意,rbind()
将 plotgrobs 组合在一起需要它们具有相同数量的列,因此您不能在一个图中省略 legend/guide 而在另一个图中不省略。不过,您可以在 gtable 中删除其中之一。