ggplot2 facet_wrap绘制每个方面的点,段,文本

时间:2017-08-22 21:51:42

标签: r ggplot2

这是与herehere相同的问题。但是,那里的解决方案似乎没有起作用。

我这里有示例数据:

library("ggplot2")
library("ggrepel")

# order of the chromosomes
chrom_order <- c("chr1", "chr2", "chr3", "chr4", "chr5", 
    "chr6", "chr7", "chr8", "chr9", "chr10", "chr11", "chr12", 
    "chr13", "chr14", "chr15", "chr16", "chr17", "chr18", "chr19", 
    "chr20", "chr21", "chr22", "chrX", "chrY", "chrM")

# data frame of chromosome sizes
chrom_sizes <- structure(list(chrom = structure(c(25L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L), .Label = c("chr1", "chr2", "chr3", 
"chr4", "chr5", "chr6", "chr7", "chr8", "chr9", "chr10", "chr11", 
"chr12", "chr13", "chr14", "chr15", "chr16", "chr17", "chr18", 
"chr19", "chr20", "chr21", "chr22", "chrX", "chrY", "chrM"), class = "factor"), 
    size = c(16571L, 249250621L, 243199373L, 198022430L, 191154276L, 
    180915260L, 171115067L, 159138663L, 146364022L, 141213431L, 
    135534747L, 135006516L, 133851895L, 115169878L, 107349540L, 
    102531392L, 90354753L, 81195210L, 78077248L, 59128983L, 63025520L, 
    48129895L, 51304566L, 155270560L, 59373566L)), .Names = c("chrom", 
"size"), row.names = c(NA, -25L), class = "data.frame")

# regions to label 
sample_cns <- structure(list(gene = c("AFF1", "ANKRD24", "ARID1A", "CDH23", 
"CDH23-AS1", "CHD5", "CTC-554D6.1", "DCC", "DOT1L", "FLT4"), 
    chromosome = structure(c(4L, 19L, 1L, 10L, 10L, 1L, 5L, 18L, 
    19L, 5L), .Label = c("chr1", "chr2", "chr3", "chr4", "chr5", 
    "chr6", "chr7", "chr8", "chr9", "chr10", "chr11", "chr12", 
    "chr13", "chr14", "chr15", "chr16", "chr17", "chr18", "chr19", 
    "chr20", "chr21", "chr22", "chrX", "chrY", "chrM"), class = "factor"), 
    start = c(87869685L, 4183350L, 27022894L, 73199588L, 73269838L, 
    6166339L, 112162804L, 49867157L, 2164183L, 180030191L), end = c(88056853L, 
    4224502L, 27107247L, 73575035L, 73270969L, 6240083L, 112179823L, 
    51057023L, 2229791L, 180076545L), log2 = c(-1.01818, -0.517649, 
    -1.14236, -0.527636, -0.527636, -1.14236, -0.438652, -0.741936, 
    -0.517649, -0.438652), depth = c(466, 155.508, 304.046, 720.821, 
    1096.83, 253.5, 871.9, 626.033, 160.42, 567.457), weight = c(17.8883, 
    17.0764, 23.296, 52.0485, 1.77117, 25.5399, 22.9053, 19.3831, 
    26.4509, 19.0353), cn = c(1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 
    1L, 1L), probes = c(587L, 462L, 1023L, 922L, 922L, 1023L, 
    753L, 465L, 462L, 753L)), .Names = c("gene", "chromosome", 
"start", "end", "log2", "depth", "weight", "cn", "probes"), row.names = c(NA, 
10L), class = "data.frame")

# base plot
p <- ggplot(data = chrom_sizes, aes(x = chrom, y = size)) + geom_bar(stat="identity", fill="grey90") + coord_flip() +
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) + facet_wrap( ~ chrom, scales = "free_y")

print(p)

产生所需的基本图:

enter image description here

但是,我接下来想要使用sample_cns数据框中的条目注释该图。但是当我尝试添加它们时,每个值都放在每个图中:

# places labels and lines on every facet
p + geom_segment(data = sample_cns, aes(x = chromosome, xend = chromosome, y = start, yend = end, colour = cn), size=13) + 
    geom_text_repel(data = sample_cns, aes(x = chromosome, y = start, label = gene))

enter image description here

根据引用的问题,我尝试传递一个单项数据框,一次添加一个注释。但是,这仍然会导致数据在每个方面都被绘制。当我尝试从头开始重新创建数据框并将其传递时,也会发生同样的情况,并且会发生文本,线段和传递的点:

# first row only; still adds to every facet
df <- sample_cns[1, ]
p + geom_segment(data = df, aes(x = chromosome, xend = chromosome, y = start, yend = end, colour = cn), size=13) + 
    geom_text_repel(data = df, aes(x = chromosome, y = start, label = gene))


# make new df from scratch
df <- data.frame(gene = "AFF1", chromosome = factor("chr4", levels = chrom_order), start = 87869685, end = 88056853, cn = 1)
p + geom_segment(data = df, aes(x = chromosome, xend = chromosome, y = start, yend = end, colour = cn), size=13) + 
    geom_text_repel(data = df, aes(x = chromosome, y = start, label = gene))

p + geom_point(data = df, aes(x = chromosome, y = start, colour = cn), size=13)

enter image description here enter image description here

有什么想法吗?我错过了什么?为什么这种技术在其他代码示例中有效,但不在这里?

我也在使用R版本3.2.3和ggplot2版本2.2.1

1 个答案:

答案 0 :(得分:2)

您必须告诉ggplot每个标签都在哪个方面。这意味着包含标签的数据框需要包含您所面对的列。

您正在使用名为chromfacet_wrap( ~ chrom)的列。您的标签cns的数据框没有名为chrom的列。添加一个名为chrom的列到cns,显示每个标签应该在哪个方面。