我会建议下一个方法。您必须创建一个组,然后使用构面。我从@AllanCameron 学到的大部分技巧对于这种风格的问题都有很好的答案。接下来是可以做到这一点的代码:
library(tidyverse)
#Data
df<-data.frame(animal=c("cat1","cat2","mouse1","mouse2","dog1","dog2"),
size=c(3,4,1,2,6,7),stringsAsFactors = F)
#Create group
df$Group <- gsub('\\d+','',df$animal)
#Now plot
ggplot(df,aes(x=animal,y=size))+
geom_col()+
facet_wrap(.~Group,scales = 'free', strip.position = "bottom")+
theme(strip.placement = "outside",
panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.text = element_text(face = "bold", size = 12),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
输出: