ggplot中是否存在R函数,以将图限制为最大数据范围?

问题描述 投票:0回答:2

奇怪的问题,但是我似乎找不到一种整齐的方法来管理它!我在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

看起来好像是我在主题部分所做的事情。不确定-但会有所帮助!

r ggplot2
2个回答
1
投票

不太清楚这个问题,您需要发布一个可复制的示例。

您是否正在询问如何限制轴范围?如果是

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

将调整x轴范围。与y相似的功能。只需调整0,1。将其添加到网格中的每个图上。

更新:

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

0
投票

[如果要使图块之间没有空格堆叠,则需要在plot.margin调用中使用theme值进行播放。您可以分配负值和其他单位。因此,类似这样的事情可以满足您的要求:

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底部或顶部边距来获得所需的内容。此外,至少在提供的示例中,我评论了主题调用的一些行,因为它们不必要。如果需要,请随时取消注释原始数据。

© www.soinside.com 2019 - 2024. All rights reserved.