如何网格化面板图,使它们在 R 中的 ggplot 中具有分类变量和不同的 x 变量

how to grid panel plots such that they have categorical variables and different x variables in ggplot in R

我的实际数据类似于下面的数据。我的代码正在生成两个单独的图,如下图中的两个顶部图。但我希望它们在一个单一的情节中,就像图片中的底部情节。

data(Salaries, package = "carData")
Salaries <- Salaries[1:15,]
Salaries$name <- c("Jim","Alex","Ray","Dog","Kim",
                   "Michael","Mike","Adrian","Claire","Lucy",
                   "Fray","Lucas","Crraig","Anthony","Harper")
Salaries <- Salaries[order(Salaries$yrs.since.phd), ]
ggplot(Salaries, aes(x = yrs.since.phd , y=reorder(name, yrs.since.phd)))+
  geom_point(size = 3, aes(colour = sex))+
  facet_grid(rows = vars(sex), scales = "free_y" ,space = "free_y")+
  theme(legend.position = "none")

Salaries <- Salaries[order(Salaries$salary), ]
ggplot(Salaries, aes(x = salary , y=reorder(name, salary)))+
  geom_point(size = 3, aes(colour = sex))+
  facet_grid(rows = vars(sex), scales = "free_y" ,space = "free_y")+
  theme(legend.position = "none")

我不知道该怎么做。感谢您对此提供的任何帮助。

希望这是你想要的

  library(ggplot2)
  library(gtable)
  library(grid)
  library(gridExtra)
  
  data(Salaries, package = "carData")
  Salaries <- Salaries[1:15,]
  Salaries$name <- c("Jim","Alex","Ray","Dog","Kim",
                     "Michael","Mike","Adrian","Claire","Lucy",
                     "Fray","Lucas","Crraig","Anthony","Harper")
  Salaries <- Salaries[order(Salaries$yrs.since.phd), ]
  p1=ggplot(Salaries, aes(x = yrs.since.phd , y=reorder(name, yrs.since.phd)))+
    geom_point(size = 3, aes(colour = sex))+
    facet_grid(rows = vars(sex), scales = "free_y" ,space = "free_y")+
    theme(legend.position = "none",strip.text.y = element_blank())
  
  Salaries <- Salaries[order(Salaries$salary), ]
  p2=ggplot(Salaries, aes(x = salary , y=reorder(name, salary)))+
    geom_point(size = 3, aes(colour = sex))+
    facet_grid(rows = vars(sex), scales = "free_y" ,space = "free_y")+
    theme(axis.text.y = element_blank(), 
          axis.ticks.y = element_blank(), 
          axis.title.y = element_blank(),
          plot.background = element_blank(),
          legend.position = "none")
  gt1 <- ggplotGrob(p1)
  gt2 <- ggplotGrob(p2)
   
  grid.arrange(gt1, gt2, ncol=2)

我的参考来自