如何在多个多方面ggplot2图形中实现相同的方面大小和比例?

NoBackingDown

我有一系列的ggplot2图形,这些图形的水平数恒定,但垂直面数不同。我想将图形另存为横向a4格式的.pdf。

但是,我不知道如何实现相同比例的构面。如果我尝试手动调整它,widthheight针对不同数量的垂直构面进行更改,则各图之间的比例会有所不同,即,我得到的点大小和线宽也不同。

本质上,对于具有可变数量(垂直)构面的图,如何实现相同的构面大小和比例?

这是一个例子:

df <- expand.grid(a = 1:2, b = 1:5, x = 1:10)
df$y <- df$x
plot <- ggplot(data = df, mapping = aes(x = x, y = y)) +
            geom_point()
plot1 <- plot + facet_grid(facets = "a ~ b")
plot2 <- plot + facet_grid(facets = ". ~ b")

ggsave(filename = "./figures/plot1.pdf", plot = plot1,
   height = 210, width = 297, units = "mm")

ggsave(filename = "./figures/plot2.pdf", plot = plot2,
   height = 210, width = 297, units = "mm")
浸礼会

我使用此代码将面板尺寸设置为绝对值,也许在这里有帮助

set_panel_size <- function(p=NULL, g=ggplotGrob(p), file=NULL, 
                           margin = unit(1,"mm"),
                           width=unit(4, "cm"), 
                           height=unit(4, "cm")){

  panels <- grep("panel", g$layout$name)
  panel_index_w<- unique(g$layout$l[panels])
  panel_index_h<- unique(g$layout$t[panels])
  nw <- length(panel_index_w)
  nh <- length(panel_index_h)

if(getRversion() < "3.3.0"){

   # the following conversion is necessary
   # because there is no `[<-`.unit method
   # so promoting to unit.list allows standard list indexing
   g$widths <- grid:::unit.list(g$widths)
   g$heights <- grid:::unit.list(g$heights)

   g$widths[panel_index_w] <-  rep(list(width),  nw)
   g$heights[panel_index_h] <- rep(list(height), nh)

} else {

   g$widths[panel_index_w] <-  rep(width,  nw)
   g$heights[panel_index_h] <- rep(height, nh)

}

  if(!is.null(file))
    ggsave(file, g, 
           width = convertWidth(sum(g$widths) + margin, 
                                unitTo = "in", valueOnly = TRUE),
           height = convertHeight(sum(g$heights) + margin,  
                                  unitTo = "in", valueOnly = TRUE))

  invisible(g)
}

print.fixed <- function(x) grid.draw(x)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用多个方面播放 ggplot2 的图形

使用ggplot2的多个方面

U2F多方面AppID在Chromium v40.x中不起作用

如何在ggplot2中的方面之间强制使用常见的x轴标签/限制?

如何在ggplot2中的各个方面注释文本

如何在交互绘图(ggplot2;交互)中的各个方面注释文本?

如何在 ggplot2 中的各个方面在绘图之外添加标题?

多方面的facet_grid变得不可读/增加地块的大小?

具有多方面应用ID的U2F

在Java中传递对象,并试图改变他们在许多方面

根据 R 中多方面时间序列图的条件自定义 alpha 值

如何在R中的ggplot2中向条形图方面添加百分比?

ggplot2问题与方面和关闭剪辑

使用ggplot2将方面标签中的新行和plotmath合并

ggplot2中每个方面的注释组均值

ggplot2中每个方面的不同功能曲线

显示每个方面组 ggplot2 中的观察数

在ggplot2直方图中的每个方面选择不同的中断(不是比例问题)

ggplot2:方面标签模糊

如何在ggplot2中指定图形大小而与轴标签无关

在ggplot2中,使用多个自变量在数据中的各个方面生成误差线

如何在R中的ggplot的每个方面添加R2?

通过ggplot中的多个方面进行回归

如何在sql中从关系的多个方面正确查询?

如何在Sitecore方面实现对象集合?

使用x和y轴在ggplot2中的日志缩放方面有所不同

天蓝色搜索。如果我有很多方面怎么办

获取不存在其他模型的多方面关系的对象?

如何在ggplot2中为树图的标签文本设置相同的大小