在不同的 2 ggplot 中划分 ggplot

问题描述

我有这个 ggplot :plot1

我使用此代码获得了此图:

   g<- ggplot(base__,aes(x=Race_name,y=ec_prem,color=nouv_grp))+scale_color_brewer(palette = "Paired")+
  geom_jitter(position=position_jitter(0.2))+xlab("Course")+ylab("Ecart / 1er (secondes)")+ylim(-1,120)+labs(colour = "Groupe PC1")+theme_minimal()+theme(axis.text.x = element_text(size = 7,angle = 90))
g

我想把它分成 2 个图,使可视化更容易理解。所以我使用了 facet_grid() :

g=ggplot(base__,angle = 90))
g+facet_grid(haha~.)

我得到了这个情节:

plot2

但我想得到 2 个不同的 x 轴,并且我希望我的抖动图不那么集中(不那么紧密)。

希望有人能给我一个解决方案。

提前致谢:)

解决方法

正如 teunbrand 所提到的,facet_grid() 不允许对齐的轴使用不同的比例(即在列中统一 x,在一行中统一 y)但是 facet_wrap() 可以帮助您解决这个问题.但是,为了让轴​​文本为每个方面打印,我认为 lemon::facet_rep_wrap() 是您需要的。请参阅下面的一个小例子。

library(tidyverse)
library(lemon)
#> 
#> Attaching package: 'lemon'
#> The following object is masked from 'package:purrr':
#> 
#>     %||%
#> The following objects are masked from 'package:ggplot2':
#> 
#>     CoordCartesian,element_render

mtcars %>%
  rownames_to_column(var = "car_name") %>%
  ggplot(aes(x = car_name,y = mpg)) +
  geom_col() +
  facet_rep_wrap(
    facets = vars(cyl),ncol = 1,repeat.tick.labels = TRUE,scales = "free"
  ) +
  theme(axis.text.x = element_text(
    angle = 90,hjust = 1,vjust = 0.5
  ))

reprex package (v1.0.0) 于 2021 年 3 月 29 日创建