У меня есть аналогичные данные, как в следующем примере:

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  

enter image description here

Я хочу добавить немного места на y_axis после каждого кластера. Например, после кластера 3 (красные) я хочу добавить некоторое пространство, такое как пространство между панелями и т. Д. На следующем графике.

введите описание изображения здесь Есть способ сделать это?

4
Indigofera suffruticosa 6 Ноя 2018 в 18:50

1 ответ

Лучший ответ

Мое решение преобразует ось 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)

enter image description here

4
Henry Cyranka 6 Ноя 2018 в 17:08