【问题标题】:How to create a back-to-back plot with a shared x-axis using ggplot?如何使用 ggplot 创建具有共享 x 轴的背靠背图?
【发布时间】:2019-10-22 10:19:37
【问题描述】:

我使用 ggplot 创建了两个单独的图,我想用一个共享的 x 轴垂直堆叠它们。我发现了很多水平图的例子(比如这个:drawing pyramid plot using R and ggplot2),所以我想知道如何垂直地做到这一点。

这是我分别创建的两个图表。

这就是我希望情节的样子:

我的数据(组合)和我用来创建呼吸图的代码(y 轴范围的 y_scale_continuous 变化):

df <- structure(list(month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 
4L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
"Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", "factor"
)), site = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("port", 
"bluff", "palm"), class = "factor"), o2 = c(-0.230806451612904, 
-0.235637733188544, -0.245057174556773, -0.164558508989615, -0.213785860472091, 
-0.0743139564452465, -0.148132987820631, -0.18420332235214, -0.506846587196195, 
-0.195636532933123, -0.19852591713126, -0.609512525218665, 0.00176680974320632, 
0.00148126891095111, 0.000339290425939857, 0.00286401267875333, 
0.00351454109668776, 0.0017904651506876, 0.00124172906552544, 
0.00296532826689073, 0.00147283622695563, 0.000615469748914681, 
0.00288144920417381, 0.000610425917237045), sd = c(0.13092880400035, 
0.0293651765468604, 0.186903186503979, 0.0165030189596949, 0.035275044506981, 
0.0117338426583851, 0.124600245537606, 0.0329422384479664, 0.321892933123791, 
0.129212671278402, 0.0629054816199097, 0.157196511439801, 8.01053634208091e-05, 
0.000676914258685707, 0.00042433026354163, 0.000120510874443334, 
0.000729790000379787, 0.000750152053448522, 0.000883147983451001, 
0.000397030811318241, 0.00107944390293209, 0.000315781661923161, 
0.00218786030744793, 0.000184295714801481), n = c(2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L), se = c(0.0925806451612918, 0.0207643154670252, 
0.132160510602338, 0.00952802243882137, 0.0203660564417815, 0.00677453721744733, 
0.0719379853022306, 0.0190192102356423, 0.185844971589259, 0.0746009705452959, 
0.0363184967467579, 0.0907574481954395, 5.66430456842669e-05, 
0.000478650662598528, 0.000300046806812961, 6.9576985800136e-05, 
0.000421344453171167, 0.000433100489991655, 0.00050988572597971, 
0.000229225845791162, 0.000623217227932945, 0.000182316627516485, 
0.00126316173745436, 0.000106403180551129), trmt = c("res", "res", 
"res", "res", "res", "res", "res", "res", "res", "res", "res", 
"res", "pro", "pro", "pro", "pro", "pro", "pro", "pro", "pro", 
"pro", "pro", "pro", "pro")), row.names = c(NA, -24L), class = "data.frame")

library(ggplot2)
    plot <- ggplot(df, aes(x=month, y=o2, fill=site))+
      geom_bar(stat="identity", color="black", width=0.4, position=position_dodge()) +
      geom_errorbar(data=subset(df, df$trmt=="res"), aes(ymin=o2-se, ymax=o2), width=0.2, position=position_dodge(0.4)) +
      scale_x_discrete("Month")+ #chronological order for plotting
      scale_y_continuous(breaks =seq(-0.8, 0, by = 0.1), expand = c(0,0), limits=c(-0.8, 0))+
      ylab(yaxis) +
      scale_fill_manual(name = "Site",
                        labels = c("Port", "Bluff", "Palm"),
                        values = c("#FC4E07","#00AFBB", "#C3D7A4"))+
      theme(panel.border = element_blank()) +
      theme_bw() +
      theme(axis.title.x = element_text(size=18, margin = margin(t=20, r=0, b=0, l=0)),
            axis.title.y = element_text(size=18, margin = margin(t=0, r=20, b=0, l=0)),
            axis.text.x = element_text(size=16, color="black"), #hjust <- l'éspace des axes
            axis.text.y = element_text(size=16, color="black"),
            legend.title = element_text(size=15),
            legend.text = element_text(size=13),
            plot.margin = unit(c(1,1,1,1), "cm")) +
      theme(axis.line = element_line(colour = "black"), #remove top and right borders
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank()) +
      geom_hline(yintercept = 0) 

感谢任何帮助或指针!

【问题讨论】:

    标签: r ggplot2 plot graph data-visualization


    【解决方案1】:

    有几种方法可以做到这一点,但使用patchwork 包可能是最直接的。

    为了让代码更清晰,我去掉了几乎所有的化妆品:

    library(dplyr)
    library(ggplot2)
    
    p1 <-
      df %>% 
      filter(trmt == "pro") %>% 
      ggplot(aes(x=month, y=o2, fill=site)) +
      geom_bar(stat="identity", color="black", width=0.4, position=position_dodge()) +
      geom_errorbar(aes(ymin=o2, ymax=o2+se), width=0.2, position=position_dodge(0.4)) +
      scale_fill_manual(name = "Site",
                        labels = c("Port", "Bluff", "Palm"),
                        values = c("#FC4E07","#00AFBB", "#C3D7A4")) +
      xlab(NULL) +
      theme_bw() +
      theme(panel.border = element_blank()) +
      geom_hline(yintercept = 0)
    
    p2 <- 
      df %>% 
      filter(trmt == "res") %>% 
      ggplot(aes(x=month, y=o2, fill=site)) +
      geom_bar(stat="identity", color="black", width=0.4, position=position_dodge()) +
      geom_errorbar(aes(ymin=o2-se, ymax=o2), width=0.2, position=position_dodge(0.4)) +
      scale_fill_manual(name = "Site",
                        labels = c("Port", "Bluff", "Palm"),
                        values = c("#FC4E07","#00AFBB", "#C3D7A4")) +
      xlab(NULL) +
      theme_bw() +
      theme(panel.border = element_blank(), axis.text.x = element_blank(),
            legend.position = "none") +
      geom_hline(yintercept = 0)
    
    
    library(patchwork)
    p1 + p2 + plot_layout(ncol = 1)
    

    【讨论】:

    • 感谢您移除化妆品。真的很有帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-03-06
    • 2013-01-22
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多