繁体   English   中英

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上添加一些空间。 例如,在第3组(红色的)之后,我想在下图中添加一些空间,如面板之间的空间等。

在此输入图像描述 有没有办法做到这一点?

我的解决方案将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)

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM