r - ggplot : placing facet strips above axis title

标签 r ggplot2

我想创建一个图,其中轴标题位于顶部/左侧,轴标签位于底部/右侧。这可以通过创建一个重复的轴并删除不需要它们的边上的轴标题和标签(通过主题选项)来完成。

然而,在将 x 轴标题放在多面图中的顶部时,我遇到了一个问题。出于某种原因,尽管使用了选项 strip.placement="outside",但刻面的条形文本仍位于轴标题下方。 .有趣的是,如果我不隐藏轴标签(见下图),它们位于条形文本下方,但轴标题位于条形文本上方。

知道如何将轴标题也移动到 strip.text 下方吗?请注意,我正在使用 ggplot ggplot2_3.3.0.9000。非常感谢。

library(tidyverse)

iris %>% 
  ggplot()+
  geom_bar(aes(x=Sepal.Length,
               y=Sepal.Width),
           stat="identity")+
  facet_wrap(~Species,
             strip.position = "top")+
  scale_y_continuous(sec.axis = dup_axis())+  #create secondary axis
  scale_x_continuous(sec.axis = dup_axis())+  #create secondary 
  theme(strip.placement = "outside",
        axis.title.x.top = element_text(hjust=0),  #left align axis title on top
        axis.title.x.bottom = element_blank(), #remove title on x axis/bottom
        axis.ticks.length.x.top = unit(0, units="cm"), #remove axis ticks on top
        axis.text.y.left = element_blank(), #remove axis label on left side
        axis.title.y.right = element_blank(), #remove axis title on right side
        axis.ticks.length.y.left = unit(0, units="cm")) #remove axis ticks on left 




sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18362)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Austria.1252  LC_CTYPE=English_Austria.1252   
#> [3] LC_MONETARY=English_Austria.1252 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Austria.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] forcats_0.4.0      stringr_1.4.0      dplyr_0.8.3        purrr_0.3.3       
#> [5] readr_1.3.1        tidyr_1.0.2        tibble_2.1.3       ggplot2_3.3.0.9000
#> [9] tidyverse_1.3.0   
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_0.2.5 xfun_0.12        haven_2.2.0      lattice_0.20-38 
#>  [5] colorspace_1.4-1 vctrs_0.2.2      generics_0.0.2   htmltools_0.4.0 
#>  [9] yaml_2.2.0       rlang_0.4.3      pillar_1.4.3     glue_1.3.1      
#> [13] withr_2.1.2      DBI_1.1.0        dbplyr_1.4.2     modelr_0.1.5    
#> [17] readxl_1.3.1     lifecycle_0.1.0  munsell_0.5.0    gtable_0.3.0    
#> [21] cellranger_1.1.0 rvest_0.3.5      evaluate_0.14    labeling_0.3    
#> [25] knitr_1.27       fansi_0.4.1      highr_0.8        broom_0.5.3     
#> [29] Rcpp_1.0.3       scales_1.1.0     backports_1.1.5  jsonlite_1.6    
#> [33] farver_2.0.3     fs_1.3.1         hms_0.5.3        digest_0.6.23   
#> [37] stringi_1.4.5    grid_3.6.1       cli_2.0.1        tools_3.6.1     
#> [41] magrittr_1.5     crayon_1.3.4     pkgconfig_2.0.3  xml2_1.2.2      
#> [45] reprex_0.3.0     lubridate_1.7.4  assertthat_0.2.1 rmarkdown_2.1   
#> [49] httr_1.4.1       R6_2.4.1         nlme_3.1-140     compiler_3.6.1

创建于 2020-02-06 由 reprex package (v0.3.0)

最佳答案

这种行为通常是需要的。这在删除 theme(strip.placement = 'outside') 时变得特别明显。 .我个人觉得这样在视觉上更清晰。

我也觉得您可能正在寻找情节标题(您可以使用 labs(title = 'Sepal.Length') 而不是 x 轴标题。

对于您的问题,我想人们无法摆脱与 grobs 的纠缠。这里有一个简单的方法可以简单地交换 gtable 中的行布局:

library(tidyverse)
library(gtable)
library(grid)

p <- 
  ggplot(iris)+
    geom_bar(aes(x=Sepal.Length, y=Sepal.Width), stat="identity")+
    facet_wrap(~Species, strip.position = "top") +
    scale_x_continuous(sec.axis = dup_axis()) +
    theme(strip.placement = "outside")

gp <- ggplotGrob(p)

# gp$layout #helps you to understand the gtable object 
# gtable_show_layout(ggplotGrob(p)) #helps you to understand the gtable object 

t_title <- gp$layout[gp$layout[['name']] == 'xlab-t' ,][['t']]
t_strip <- gp$layout[grepl('strip', gp$layout[['name']]),][['t']]

gp$layout[gp$layout[['name']] == 'xlab-t' ,][['t']] <- unique(t_strip)
gp$layout[gp$layout[['name']] == 'xlab-t' ,][['b']] <- unique(t_strip)
gp$layout[grepl('strip', gp$layout[['name']]),][['t']] <- t_title
gp$layout[grepl('strip', gp$layout[['name']]),][['b']] <- t_title

grid.newpage()
grid.draw(gp)



创建于 2020-02-06 由 reprex package (v0.3.0)

关于r - ggplot : placing facet strips above axis title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60091106/

相关文章:

r - 如何安装不在 conda 存储库中的 R 软件包?

r - 在 R 中,将数据框对角线转换为行

r - R plot() 或 ggplot2() 中的对数 y 轴刻度线

R:将 alpha 包添加到 2d 或 3d 散点图

R 使用字符串作为列名称将字符串拆分为列,并使用任何数字作为这些列中的值

r - 如何在R中的函数内索引数据框列

r - ggplot2:如何在刻度线上结束 y 轴?

r - 如何在百分比条形图中显示平均值和误差条

r - 分组变量上的 ggplot 和 ggsignif 错误

r - 使用 ggcorrplot 包更改相关图对角线的方向