ggplot2 facet_wrap在每个构面上绘制点,线段和文本 [英] ggplot2 facet_wrap plotting points, segments, text in every facet

查看:76
本文介绍了ggplot2 facet_wrap在每个构面上绘制点,线段和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与

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

 #在每个方面都放置标签和线条p + geom_segment(数据= sample_cns,aes(x =染色体,xend =染色体,y =开始,yend =结束,颜色= cn),大小= 13)+geom_text_repel(数据= sample_cns,aes(x =染色体,y =起始,标签=基因)) 

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

 #仅第一行;仍然增加了每个方面df<-sample_cns [1,]p + geom_segment(数据= df,aes(x =染色体,xend =染色体,y =开始,yend =结束,color = cn),大小= 13)+geom_text_repel(数据= df,aes(x =染色体,y =起始,标签=基因))#从头开始创建新的dfdf<-data.frame(基因="AFF1",染色体=因子("chr4",级别= chrom_order),开始= 87869685,结束= 88056853,cn = 1)p + geom_segment(数据= df,aes(x =染色体,xend =染色体,y =开始,yend =结束,color = cn),大小= 13)+geom_text_repel(数据= df,aes(x =染色体,y =起始,标签=基因))p + geom_point(数据= df,aes(x =染色体,y =起始,颜色= cn),大小= 13) 

有什么想法吗?我想念什么?为什么在其他代码示例中也能使用这种相同的技术,而在这里却不行?

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

解决方案

您必须告诉 ggplot 每个标签进入哪个方面.这意味着包含标签的数据框必须具有列(s)你面朝上.

您正在通过名为 chrom facet_wrap(〜chrom)的列进行构面.标签的数据框 cns 没有名为 chrom 的列.在 cns 上添加一列 chrom ,以显示每个标签应位于哪个方面.

This is the same issue that has been posted here and here. However, the solutions there do not appear to be working.

I have example data here:

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)

Which produces the desired base plot shown here:

However, I next want to annotate the plot with the entries in the sample_cns dataframe. But when I try to add them, every value gets placed in every plot:

# 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))

As per the referrenced questions, I tried passing a single-entry dataframe to add the annotations one at a time. However, this still results in the data being plotted in every facet. The same happens when I try to re-create the dataframe from scratch and pass it instead, and it happens with text, line segments, and points being passed:

# 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)

Any ideas? What am I missing? Why does this same technique work in those other code samples, but not here?

Also I am using R version 3.2.3, and ggplot2 version 2.2.1

解决方案

You have to tell ggplot which facet each labels go in. This means the data frame containing the labels needs to have the column(s) you facet on.

You are faceting by a column called chrom, facet_wrap( ~ chrom). Your data frame for the labels, cns, does not have a column called chrom. Add a column called chrom to cns that show which facet each label should be in.

这篇关于ggplot2 facet_wrap在每个构面上绘制点,线段和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆