用于绘制多个紧密对齐的绘图的R代码吗?

我正在尝试将多个条形图和散点图紧密地绘制在一起(即没有间隙将各图分开)。我正在绘制两条不同河流的流量和盐度值,尽管年份中有一年前/一年后的数据,但要使年份保持一致是很重要的。我希望没有间隙,坐标轴刻度标签,坐标轴标签,绘图之间的间隔,尽管每个绘图的刻度线都有助于使其可读。我还希望在整个过程中使用非常浅的灰色垂直线,以便于比较给定年份的两条河流的不同值。

我没有尝试在R基本计划和ggplot2中执行此操作,但都无法解决。我无法找到一种简单的方法来对齐X轴(它们不会从点到条形图以及从河流到河流对齐),也无法使这些图垂直相邻。

我想将输出导出为多面板.png图像。

这是示例数据,以及到目前为止我在ggplot中如何尝试这样做:

river1 = data.frame(year     =  1991:2017, 
                    flow     =  rnorm(n=27, mean = 900, sd = 350),
                    salinity =  rnorm(n=27, mean = 3000, sd = 800))
river2 = data.frame(year     =  1992:2019, 
                    flow     =  rnorm(n=28, mean = 20000, sd = 2400),
                    salinity =  rnorm(n=28, mean = 700, sd = 1500))

library(ggplot2)
(r1sal = ggplot(river1, aes(x = year, y = salinity)) + 
    geom_point(stat = "identity", color="darkred", fill="darkred", size = 2) + 
    labs(title = "River 1 flow and flow-weighted salinity", x = "", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r1flow = ggplot(river1, aes(x = year, y = flow)) + 
    geom_bar(stat = "identity", color="blue", fill="lightblue") + 
    labs(x = "Water year", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r2sal = ggplot(river2, aes(x = year, y = salinity)) + 
    geom_point(stat = "identity", color="darkred", fill="darkred", size = 2) + 
    labs(title = "River 2 flow and flow-weighted salinity", x = "Water year", y = "Streamflow (ML)") +
    scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

(r2flow = ggplot(river2, aes(x=year, y=flow)) + 
       geom_bar(stat = "identity", color="blue", fill="lightblue") + 
       labs( x = "Water year", y = "Streamflow (ML)") +
       scale_x_continuous(breaks= seq(from=1991, to= 2019, by=2)) + theme_classic())

library(ggpubr)
ggarrange(r1sal, r1flow, r2sal, r2flow,
            labels = c("A", "B", "C","D"),
            ncol = 1, nrow = 4)

我知道一定有一个简单的方法可以做到这一点。非常感谢您的协助!

评论
  • 奥巴喵
    奥巴喵 回复

    我会考虑使用方面。

    river1 = data.frame(year     =  1991:2017, 
                        flow     =  rnorm(n=27, mean = 900, sd = 350),
                        salinity =  rnorm(n=27, mean = 3000, sd = 800))
    river2 = data.frame(year     =  1992:2019, 
                        flow     =  rnorm(n=28, mean = 20000, sd = 2400),
                        salinity =  rnorm(n=28, mean = 700, sd = 1500))
    
    
    library(tidyverse)
    
    rivers <- bind_rows(river1 = river1, river2 = river2, .id = "River") %>% 
      pivot_longer(cols = c("flow", "salinity"), names_to = "variable", values_to = "value") 
    
    ggplot(data = filter(rivers, variable == "salinity" ), mapping = aes(x = year, y = value)) +
      geom_col(data = filter(rivers, variable == "flow" ), fill = "blue") +
      geom_point(color="darkred") +
      geom_line(color="darkred") +
      facet_grid(River + variable ~ ., scales = "free_y")
    

    Created on 2020-05-11 by the reprex package (v0.3.0)

    The alternative would be to play with themes to set axis.text to element_blank() and then combine the figures (I would do this with patchwork package)