在 R 中创建具有相同 x 轴的不同几何表示的构面图

Create a facet plot with different geom representations with same x-axis in R

我想创建一个分面图,它共享相同的 x 轴但具有不同的 y 轴和不同的几何图形,例如,像这样:

这是一个可重现的例子,我按照上面的方法生成了两个独立的分面图

library(ggplot2)

## create example data similar structure to my own
data(iris)
a <- iris
a$Species <- paste0(a$Species, "_a")
b <- iris
b$Species <- paste0(b$Species, "_b")
c <- iris
c$Species <- paste0(c$Species, "_c")
plot_data <- rbind(a, b, c)
plot_data$rep <- c(rep("a", nrow(iris)), rep("b", nrow(iris)), rep("c", nrow(iris)))

## facet boxplot
g1 <- ggplot() + geom_boxplot(data = plot_data, aes(Species, Sepal.Width, group = rep)) +
   theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
   facet_wrap(rep~., scales = "free_x") + xlab("")

## facet heatmap
g2 <- ggplot() + geom_tile(data = plot_data, 
                           aes(factor(Species), rep, fill=Sepal.Width)) +
  scale_fill_continuous(low="white", high="#56B4E9", name="Sepal width") +
  facet_wrap(rep~., scales = "free_x") +
  theme(text=element_text(size=12),
        axis.text.x=element_text(angle=90, vjust=1, hjust=1),
        aspect.ratio=1)

## arrange
library(gridExtra)
grid.arrange(g1, g2, nrow = 2)  

这会产生这个:

我希望图表对齐并且图例偏移到右侧,如第一张图片所示(使用狡猾的剪切和粘贴作业放在一起)。最终这些情节将被传递给一个闪亮的应用程序。

我看到 this post 可能会提供一个解决方案,但我真正想知道的是,上面的内容是否可以单独使用分面,或者是否有更简单的解决方案。

非常感谢。

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats4    parallel  stats     graphics  grDevices utils     datasets 
[8] methods   base     

other attached packages:
 [1] gridExtra_2.3        plotly_4.9.2.1       pRolocdata_1.26.0   
 [4] tidyr_1.1.0          reshape2_1.4.4       pRoloc_1.29.0       
 [7] BiocParallel_1.22.0  MLInterfaces_1.68.0  cluster_2.1.0       
[10] annotate_1.66.0      XML_3.99-0.3         AnnotationDbi_1.50.0
[13] IRanges_2.22.2       MSnbase_2.14.2       ProtGenerics_1.20.0 
[16] S4Vectors_0.26.1     mzR_2.22.0           Rcpp_1.0.4.6        
[19] Biobase_2.48.0       BiocGenerics_0.34.0  ggplot2_3.3.1       
[22] shinyhelper_0.3.2    colorspace_1.4-1     colourpicker_1.0    
[25] shinythemes_1.1.2    DT_0.13              shiny_1.4.0.2       
[28] dplyr_1.0.0         

最快的解决方法就是将图例放在底部。但这里有一个拼凑而成的解决方案。

请注意,出于方便和减少代码的目的,我将适用于所有绘图的“全局”主题选项添加到对 patchwork 的调用中。这也使得在需要时更改内容变得更加容易。

library(tidyverse)
library(patchwork)

ls_iris <- replicate(3, iris, simplify = FALSE)
names(ls_iris) <- letters[1:3]
plot_data <- 
  bind_rows(map2( ls_iris, letters[1:3], function(x, y) { 
  x[["Species"]] <- paste(x[["Species"]], y, sep ="_"); x}), .id = "rep")

## facet boxplot
g1 <- ggplot() + geom_boxplot(data = plot_data, aes(Species, Sepal.Width, group = rep)) +
  facet_wrap(rep~., scales = "free_x") +
  theme(axis.text.x = element_blank(), axis.title.x = element_blank()) # remove x labels and title

## facet heatmap
g2 <- ggplot() + geom_tile(data = plot_data, 
                           aes(factor(Species), rep, fill=Sepal.Width)) +
  scale_fill_continuous(low="white", high="#56B4E9", name="Sepal width") +
  facet_wrap(rep~., scales = "free_x") +
  theme(axis.text.x=element_text(angle=90, vjust=1, hjust=1), 
        strip.background = element_blank(),
        strip.text = element_blank()) # remove facet strips
g1/g2 & 
  theme(legend.position = "bottom") # theme elements that are for both plots are more conveniently passed to the patchwork call. 

reprex package (v0.3.0)

于 2020-07-09 创建