ggplot2 facet_grid 在 y 轴上创建面板

ggplot2 facet_grid create panels on the y-axis

我有类似以下示例的数据:

dat1 <- data.frame(group=c("a", "a","a", "a","a","a","b","b","b","b","b", "b", "b","b","b","c","c","c","c","c","c"),
                   subgroup=c(paste0("R", rep(1:6)),paste0("R", rep(1:9)),paste0("R", rep(1:6))),
                   value=c(15,16,12,12,14,5,14,27,20,23,14,10,20,22,14,15,18,14,23,30,32),
                   pp=c("AT","BT","CT","AA","CC","SE","DN","AS","MM","XT","QQ","HH","MK","HT","dd","US","AG","TT","ZZ","XK","RU"),
                   clusters=c(rep("cluster1",6),rep("cluster2",9),rep("cluster3",6)))

colors <- c(rep("#74c1e8",6),rep("#808000",9),rep("#FF69B4",6))
names(colors) <- c("cluster1","cluster2","cluster3")

我的代码是:

pl <- ggplot(dat1, aes(y = pp, x = subgroup)) 
       + geom_point(aes(size=value)) 
       + facet_grid(~group, scales="free_x", space  = "free")
       + ylab("names") 
       + xlab(" ") 
       + theme(axis.text.y = element_text(color=colors))

pl  

我想要的是在每个簇后的 y_axis 上添加一些 space。例如,在集群 3(红色)之后,我想在下图中的面板之间添加一些 space,例如 space 等。

有没有办法做到这一点?

我的解决方案将 y 轴转换为一个因子并在每个簇之间添加 geom_hline

library(tidyverse)
dat1 <- data.frame(group=c("a", "a","a", "a","a","a","b","b","b","b","b", "b", "b","b","b","c","c","c","c","c","c"),
                   subgroup=c(paste0("R", rep(1:6)),paste0("R", rep(1:9)),paste0("R", rep(1:6))),
                   value=c(15,16,12,12,14,5,14,27,20,23,14,10,20,22,14,15,18,14,23,30,32),
                   pp=c("AT","BT","CT","AA","CC","SE","DN","AS","MM","XT","QQ","HH","MK","HT","dd","US","AG","TT","ZZ","XK","RU"),
                   clusters=c(rep("cluster1",6),rep("cluster2",9),rep("cluster3",6)))

colors <- c(rep("#74c1e8",6),rep("#808000",9),rep("#FF69B4",6))
names(colors) <- c("cluster1","cluster2","cluster3")




ggplot(dat1, aes(y = factor(pp), x = subgroup)) + geom_point(aes(size=value)) + facet_grid(~group, scales="free_x", space  = "free")+ 
    ylab("names") + 
    xlab(" ") + 
    theme(axis.text.y = element_text(color=colors)) + 
  geom_hline(yintercept = 15.5, color = "white", size = 2) + 
  geom_hline(yintercept = 6.5, color = "white", size = 2)