保留geom_rect()中的顺序

时间:2018-01-02 21:07:18

标签: r ggplot2

我陷入两难境地。在我的excel csv文件中,我有一个按特定顺序的序列列表,我编写了一个代码来显示序列,希望保留订单。但是,如下所示,订单不会保留。

enter image description here

但是正如你在这里看到的那样,序列是根据它们的"类型"来排列的。有没有办法改变它,并按照与数据文件相同的顺序绘制它们?

enter image description here

我的代码是:

ggplot() + 
    geom_rect(data = data, 
              mapping = aes(ymin = 0, ymax = 1, xmin = Start, xmax = End, fill = Type)) + 
    facet_grid(Seqname ~ .) + 
    theme(axis.text.y = element_blank(), 
          axis.ticks.y = element_blank())

这里我也使用dput提供数据样本。

> dput(head(data,20))
structure(list(Seqname = structure(c(4L, 4L, 4L, 4L, 4L, 64L, 
60L, 50L, 50L, 50L, 50L, 50L, 65L, 65L, 71L, 63L, 63L, 63L, 62L, 
62L), .Label = c("deletionandhypermutR0080_HIV_AG8_CONTIG_1202_p1", 
"deletionandhypermutR0102_HIV_AA6_CONTIG_3034_p1", "deletionandhypermutR0105_HIV_AA9_CONTIG_1201_p1", 
"deletionandhypermutR0116_HIV_AB8_CONTIG_4375_p1", "deletionandhypermutR0130_HIV_AC10_CONTIG_1202_p1", 
"deletionandhypermutR0231_HIV_BD4_CONTIG_1202_p1", "deletionandhypermutR0233_HIV_BD6_CONTIG_1202_p1", 
"deletionandhypermutR0234_HIV_BD7_CONTIG_1202_p1", "deletionandhypermutR0235_HIV_BD8_CONTIG_1202_p1", 
"deletionandhypermutR0236_HIV_BD9_CONTIG_1202_p1", "deletionandhypermutR0239_HIV_BD12_CONTIG_1202_p1", 
"deletionandhypermutR0240_HIV_BE1_CONTIG_1151_p1", "deletionandhypermutR0242_HIV_BE3_CONTIG_1202_p1", 
"deletionandhypermutR0244_HIV_BE5_CONTIG_1202_p1", "deletionandhypermutR0248_HIV_BE9_CONTIG_1956_p1", 
"deletionandhypermutR0251_HIV_BE12_CONTIG_1202_p1", "deletionandhypermutR0252_HIV_BF1_CONTIG_1202_p1", 
"deletionandhypermutR0253_HIV_BF2_CONTIG_1202_p1", "deletionandhypermutR0254_HIV_BF3_CONTIG_1202_p1", 
"deletionandhypermutR0257_HIV_BF6_CONTIG_1202_p1", "deletionandhypermutR0258_HIV_BF7_CONTIG_1202_p1", 
"deletionandhypermutR0265_HIV_BG2_CONTIG_1202_p1", "deletionandhypermutR0268_HIV_BG5_CONTIG_1201_p1", 
"deletionandhypermutR0273_HIV_BG10_CONTIG_1201_p1", "deletionandhypermutR0276_HIV_BH1_CONTIG_1202_p1", 
"deletionandinversionB6_Cp35pt5_1sampleB8_CB6_CONTIG_1103_p1", 
"deletionandinversionB8_Cp35pt5_1sampleC5_CB8_CONTIG_6627_p1", 
"deletionandinversionR0079_HIV_AG7_CONTIG_1117_p1", "deletionandinversionR0104_HIV_AA8_CONTIG_1155_p1", 
"deletionandinversionR0175_HIV_AG7_CONTIG_1888_p1", "deletionandinversionR0177_HIV_AG9_CONTIG_1103_p1", 
"deletionandinversionR0232_HIV_BD5_CONTIG_3686_p1", "deletionandinversionR0238_HIV_BD11_CONTIG_3686_p1", 
"deletionandinversionR0245_HIV_BE6_CONTIG_1103_p1", "deletionandinversionR0255_HIV_BF4_CONTIG_1124_p1", 
"deletionandinversionR0259_HIV_BF8_CONTIG_1103_p1", "deletionandinversionR0261_HIV_BF10_CONTIG_1103_p1", 
"deletionandinversionR0262_HIV_BF11_CONTIG_3686_p1", "deletionandinversionR0263_HIV_BF12_CONTIG_1124_p1", 
"deletionandinversionR0270_HIV_BG7_CONTIG_1103_p1", "HypermutB10_Cp35pt5_1sampleC8_CB10_CONTIG_9019_p1", 
"HypermutB12_Cp35pt5_1sampleE2_CB12_CONTIG_9019_p1", "HypermutB5_Cp35pt5_1sampleA10_CB5_CONTIG_9019_p1", 
"HypermutB7_Cp35pt5_1sampleC2_AE11_CONTIG_9004_p1", "HypermutC3_Cp35pt5_1sampleF5_CC3_CONTIG_9010_p1", 
"HypermutR0126_HIV_AC6_CONTIG_9019_p1", "HypermutR0135_HIV_AD3_CONTIG_9019_p1", 
"HypermutR0201_HIV_BA10_CONTIG_9020_p1", "IntactR0176_HIV_AG8_CONTIG_9004_p1", 
"LargedeletionB11_Cp35pt5_1sampleC9_CB11_CONTIG_4324_p1", "LargedeletionB9_Cp35pt5_1sampleC6_CB9_CONTIG_7287_p1", 
"LargedeletionC4_Cp35pt5_1sampleG8_CC4_CONTIG_277_p1", "LargedeletionF3_Cp34pt5_1sampleB4_CF3_CONTIG_1042_p1", 
"LargedeletionR0099_HIV_AA3_CONTIG_775_p1", "LargedeletionR0100_HIV_AA4_CONTIG_5077_p1", 
"LargedeletionR0101_HIV_AA5_CONTIG_2639_p1", "LargedeletionR0103_HIV_AA7_CONTIG_6233_p1", 
"LargedeletionR0106_HIV_AA10_CONTIG_5882_p1", "LargedeletionR0127_HIV_AC7_CONTIG_6333_p1", 
"LargedeletionR0129_HIV_AC9_CONTIG_888_p1", "LargedeletionR0241_HIV_BE2_CONTIG_1086_p1", 
"LargedeletionR0243_HIV_BE4_CONTIG_1371_p1", "LargedeletionR0246_HIV_BE7_CONTIG_2881_p1", 
"LargedeletionR0247_HIV_BE8_CONTIG_794_p1", "LargedeletionR0256_HIV_BF5_CONTIG_1153_p1", 
"LargedeletionR0260_HIV_BF9_CONTIG_3119_p1", "LargedeletionR0266_HIV_BG3_CONTIG_437_p1", 
"LargedeletionR0267_HIV_BG4_CONTIG_351_p1", "LargedeletionR0269_HIV_BG6_CONTIG_978_p1", 
"LargedeletionR0271_HIV_BG8_CONTIG_1486_p1", "LargedeletionR0272_HIV_BG9_CONTIG_303_p1", 
"LargedeletionR0274_HIV_BG11_CONTIG_2748_p1", "LargedeletionR0275_HIV_BG12_CONTIG_350_p1"
), class = "factor"), Start = c(638L, 1638L, 2638L, 3638L, 4638L, 
8845L, 637L, 638L, 1638L, 2638L, 3638L, 4638L, 637L, 9483L, 9316L, 
637L, 1637L, 2637L, 637L, 1637L), End = c(1637L, 2637L, 3637L, 
4637L, 5005L, 9633L, 1542L, 1637L, 2637L, 3637L, 4637L, 4964L, 
1636L, 9633L, 9633L, 1636L, 2636L, 3511L, 1636L, 2000L), Type = structure(c(1L, 
1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L), .Label = c("Deletion and hypermut", "Deletion and inversion", 
"Hypermut", "Intact", "Large deletion"), class = "factor")), .Names = c("Seqname", 
"Start", "End", "Type"), row.names = c(NA, 20L), class = "data.frame")

1 个答案:

答案 0 :(得分:4)

正如我在评论中所说,ggplot将字符列视为因子并按字母顺序对其级别进行排序。改变绘制顺序必须改变相应因子的级别顺序。在这种情况下Seqname。有几种方法可以实现。可能最简单的是使用forcats::fct_inorder来完成这类任务。

这是一种方法:

library(tidyverse)
df <-  droplevels.data.frame(df) #first to remove all the empty levels from your example

df %>%
  mutate(Seqname = forcats::fct_inorder(Seqname)) %>%
  ggplot()+ 
  geom_rect(aes(ymin = 0, ymax = 1, xmin = Start, xmax = End, fill = Type)) + 
  facet_grid(Seqname ~ .) + 
  theme(axis.text.y = element_blank(), 
        axis.ticks.y = element_blank(),
        strip.text.y = element_text(angle=0))

enter image description here

没有级别重新排序,情节看起来像这样:

enter image description here

例如,通过更改类型级别的顺序,您可以更改图例顺序:

df %>%
  mutate(Seqname = forcats::fct_inorder(Seqname),
         Type = relevel(Type, ref = "Large deletion")) %>%
  ggplot()+ 
  geom_rect(aes(ymin = 0, ymax = 1, xmin = Start, xmax = End, fill = Type)) + 
  facet_grid(Seqname ~ .) + 
  theme(axis.text.y = element_blank(), 
        axis.ticks.y = element_blank(),
        strip.text.y = element_text(angle=0))

enter image description here

我添加了一个方面文本的额外轮换。