R 在进行“排列”时是否可以修剪地块?
我有一个类似于以下的用例,其中我创建了多个绘图,并使用R 在进行“排列”时是否可以修剪地块?,r,ggplot2,gridextra,R,Ggplot2,Gridextra,我有一个类似于以下的用例,其中我创建了多个绘图,并使用gridExtra将它们排列到一些页面布局中,最后使用ggsave将其保存为PDF格式: p1 <- generate_ggplot1(...) p2 <- generate_ggplot2(...) final <- gridExtra::arrangeGrob(p1, p2, ...) ggplot2::ggsave(filename=output.file,plot=final, ...) 输出为: 为此,需要一个
gridExtra
将它们排列到一些页面布局中,最后使用ggsave
将其保存为PDF格式:
p1 <- generate_ggplot1(...)
p2 <- generate_ggplot2(...)
final <- gridExtra::arrangeGrob(p1, p2, ...)
ggplot2::ggsave(filename=output.file,plot=final, ...)
输出为:
为此,需要一个由多个部分组成的解决方案 首先,要更改单个图形中的页边距,在主题()中使用plot.margin是一种方便的方法。然而,如果目标是合并多个绘图,仅此一点无法解决问题 为此,需要在arrangeGrob()中组合plot.margin和特定的打印顺序。您需要一个特定的顺序,因为打印的顺序是您调用它们的顺序,因此,更改在其他打印后面而不是在打印前面分层的打印的边距会更容易。我们可以把它想象成覆盖我们想要缩小的绘图页边距,在我们想要缩小的绘图页边距上展开绘图页边距。请参见下图以了解说明: 在plot.margin设置之前: 在plot.margin设置之后: 组合plot.margin设置和arrangeGrob()后重新排序:
#第三个图形的主代码:
p1在使用arrangeGrob
进行排列之前,如何使用theme
修改绘图页边距?我知道,但这还不够,看到这个问题的答案了吗?问题就出在这里:我已经想尽一切办法摆脱了垂直空间,但到目前为止运气都不好。我想gridextra在布局中放置时,或者为了这个目的操纵视口时,可能有一些负面的边距来裁剪绘图区域。你能吗提供我们可以使用的两个样本图的数据和代码?我担心我的手会弄脏,然后分叉gridExtra
和family:D
library(ggplot2); library(dplyr); library(stringr); library(gridExtra)
df <- data.frame(group = c("Cars", "Trucks", "Motorbikes"),n = c(25, 25, 50),
label2=c("Cars are blah blah blah", "Trucks some of the best in town", "Motorbikes are great if you ..."))
df$ymax = cumsum(df$n)
df$ymin = cumsum(df$n)-df$n
df$ypos = df$ymin+df$n/2
df$hjust = c(0,0,1)
p1 <- ggplot(mtcars,aes(x=1:nrow(mtcars),y=mpg)) + geom_point()
p2 <- ggplot(df %>%
mutate(label2 = str_wrap(label2, width = 10)), #change width to adjust width of annotations
aes(x="", y=n, fill=group)) +
geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) +
expand_limits(x = c(2, 4)) + #change x-axis range limits here
# no change to theme
theme(axis.title=element_blank(),axis.text=element_blank(),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid=element_blank(),
axis.ticks.length=unit(0,"cm"),axis.ticks.margin=unit(0,"cm"),
legend.position="none",panel.spacing=unit(0,"lines"),
plot.margin=unit(c(0,0,0,0),"lines"),complete=TRUE) +
geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) +
coord_polar("y", start=0) +
scale_x_discrete()
final <- arrangeGrob(p1,p2,layout_matrix = rbind(c(1),c(2)),
widths=c(4),heights=c(4,4), padding=0.0,
respect=TRUE, clip="on")
plot(final)
#Main code for the 1st graph can be found in the original question.
#Main code for 2nd graph:
ggplot(df %>%
mutate(label2 = str_wrap(label2, width = 10)),
aes(x="", y=n, fill=group)) +
geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) +
geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) +
coord_polar(theta='y') +
expand_limits(x = c(2, 4)) +
guides(fill=guide_legend(override.aes=list(colour=NA))) +
theme(axis.line = element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text.y=element_blank(),
axis.text.x=element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
plot.margin = unit(c(-2, 0, -2, -.1), "cm"),
legend.position = "none") +
scale_x_discrete(limits=c(0, 1))
#Main code for 3rd graph:
p1 <- ggplot(mtcars,aes(x=1:nrow(mtcars),y=mpg)) + geom_point()
p2 <- ggplot(df %>%
mutate(label2 = str_wrap(label2, width = 10)), #change width to adjust width of annotations
aes(x="", y=n, fill=group)) +
geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) +
geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) +
coord_polar(theta='y') +
expand_limits(x = c(2, 4)) + #change x-axis range limits here
guides(fill=guide_legend(override.aes=list(colour=NA))) +
theme(axis.line = element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text.y=element_blank(),
axis.text.x=element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white"),
plot.margin = unit(c(-2, 0, -2, -.1), "cm"),
legend.position = "none") +
scale_x_discrete(limits=c(0, 1))
final <- arrangeGrob(p2,p1,layout_matrix = rbind(c(1),c(2)),
widths=c(4),heights=c(2.5,4),respect=TRUE)