【问题标题】:Is there an R function in ggplot to limit a plot to the maximum data range?ggplot 中是否有 R 函数可以将绘图限制在最大数据范围内?
【发布时间】:2019-11-24 01:06:41
【问题描述】:

奇怪的问题,但我似乎找不到一种巧妙的方法来管理它!我在 R 中制作了许多科学数字——每个都有一大堆图(通过 ggplot2)堆叠在一起(plot_grid),沿着相同的 x 轴匹配。到目前为止一切顺利,我几乎准备好导出和润色 illustrator 中的所有数字。

最后一步是对每个图施加限制,以使堆栈中每个图之间的空间尽可能窄。我已经通过以下方式完成了它:

test1<-ggplot(mtcars, aes(mpg, hp)) + 
  geom_line() +
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
        axis.line.y.right = element_line(colour = "black", size=1), 
        axis.line.y.left = element_line(colour = "black", size=1),
        axis.ticks.y.right = element_blank(),
        axis.text.y.right = element_blank(),        
        axis.title.x = element_blank(),    
        axis.ticks.x = element_blank(),    
        axis.line.x.bottom = element_blank(),    
        axis.text.x =element_blank(),
        axis.line = element_line(colour = "black", size =1),)

test2<-ggplot(mtcars, aes(mpg, drat)) + 
  geom_line() +
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
        axis.line.y.right = element_line(colour = "black", size=1), 
        axis.line.y.left = element_line(colour = "black", size=1),
        axis.ticks.y.right = element_blank(),
        axis.text.y.right = element_blank(),        
        axis.title.x = element_blank(),    
        axis.ticks.x = element_blank(),    
        axis.line.x.bottom = element_blank(),    
        axis.text.x =element_blank(),
        axis.line = element_line(colour = "black", size =1),)

test3<-ggplot(mtcars, aes(mpg, wt)) + 
  geom_line() + 
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
                      axis.line.y.right = element_line(colour = "black", size=1), 
                      axis.line.y.left = element_line(colour = "black", size=1),
                      axis.ticks.y.right = element_blank(),
                      axis.text.y.right = element_blank(),        
                      axis.title.x = element_blank(),    
                      axis.ticks.x = element_blank(),    
                      axis.line.x.bottom = element_blank(),    
                      axis.text.x =element_blank(),
                      axis.line = element_line(colour = "black", size =1),)

test<- plot_grid(test1, test2, test3, ncol=1, align="v", rel_heights = c(1,1,1))
test

这看起来像是我在主题位中所做的。不确定——但我们将不胜感激!

【问题讨论】:

  • 你能发一个reproducible example吗?如果没有数据、代码或输出,我们就只能凭想象来了解这些图表是什么以及您要做什么。 grid_plot 是什么?
  • 嗨,卡米尔——感谢您的回复。根据工作规则,我实际上无法发布任何数据,代码或输出,但我可能可以更好地解释!我有 7 个用 ggplot 制作的简单线图,plot_grid(哎呀!)是我用来将它们垂直堆叠成一个图形的工具。这 7 个被排列在这个“堆栈”中,因为它们之间的 x 轴是一致的。将其视为双 y 图,但并非像大多数双 y 图那样难以阅读。我想通过删除 ggplot 在绘图周围自动生成的任何类型的缓冲区来限制 7 个绘图之间的空间。
  • 点击我发布的链接。您不必发布您的确切数据——您可以制作虚拟数据或使用随常用软件包提供的数据。但是现在我们可以用来帮助您的东西很少,或者这会对未来的用户有所帮助
  • 已编辑!这应该足以理解问题了吗?

标签: r ggplot2


【解决方案1】:

如果您希望地块之间没有空间堆叠,您需要在theme 调用中使用plot.margin 值。您可以指定负值和不同的单位。所以,这样的事情可以做你想做的事:

library(ggplot2)
library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************

my_theme <- theme(
  plot.margin = unit(c(0, 0, -0.3, 0), "lines"),
  axis.title.x = element_blank(),    
  # axis.line = element_line(colour = "black", size =1),
  axis.line.y.right = element_line(colour = "black", size=1), 
  axis.line.y.left = element_line(colour = "black", size=1),
  axis.line.x.bottom = element_blank(),
  # axis.ticks.y.right = element_blank(),
  # axis.ticks.x = element_blank(),    
  # axis.text.y.right = element_blank(),        
  axis.text.x =element_blank(),
)

test1<-ggplot(mtcars, aes(mpg, hp)) + 
  geom_line() +
  my_theme

test2<-ggplot(mtcars, aes(mpg, drat)) + 
  geom_line() +
  my_theme

test3<-ggplot(mtcars, aes(mpg, wt)) + 
  geom_line() + 
  my_theme

test<- plot_grid(test1, test2, test3, ncol=1, align="v", rel_heights = c(1,1,1))
test

reprex package (v0.3.0) 于 2019 年 11 月 24 日创建

只需使用plot.margin 底部或顶部边距即可获得所需的内容。 另外,我评论了您的主题电话的一些行,因为它们是不必要的,至少在提供的示例中是这样。如果您的原始数据需要,请随时取消注释。

【讨论】:

    【解决方案2】:

    不是很明白这个问题,你需要发布一个可重现的例子。

    您是在问如何限制轴范围?如果是这样

    + scale_x_continuous(limits = c(0, 1))
    

    将调整 x 轴范围。 y 的类似函数。只需调整 0,1。为网格中的每个图添加它。

    更新:

    gglist <- lapply(gglist, function(x) x + scale_x_continuous(limits = c(0, 1)) )
    

    【讨论】:

    • 是的,这离我的问题越来越近了!我希望了解为什么每个堆叠的地块周围都有空隙。我只是想看看是否有办法为每个情节添加 y 限制,因为总共有近 50 个!
    • 好的 - 您可以编写一个函数,该函数采用 ggplot 并添加 scale_x_continuous。然后遍历网格中的所有 ggplots 以应用它
    • 嗯,这似乎没有解决它。 scale_x_continuous 位不会消除在垂直方向上添加的间隙 - scale_y_continuous 也不会,因为我仍然必须知道我正在绘制的特定数据范围的限制,然后为每个单独的部分输入它们。
    • 您需要像其他人所说的那样创建一个可重现的示例
    • 是的——它已经在上面了!如果我们正在查看我包含的示例,我想减少 hp 和 drat 图之间的空间,以便绘制的线几乎接触
    猜你喜欢
    • 2017-01-22
    • 2012-04-29
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2021-04-25
    • 2016-08-15
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多