用虚线连接多个面中的多个点

时间:2018-06-27 07:11:23

标签: r ggplot2

我有以下数据。每个观察结果都是一个具有拷贝数变化(copy.number.type)的基因组坐标,可以在一定比例的样品(per.found)中找到。

chr<-c('1','12','2','12','12','4','2','X','12','12','16','16','16','5'
              ,'4','16','X','16','16','4','1','5','2','4','5','X','X','X','4',
              '1','16','16','1','4','4','12','2','X','1','16','16','2','1','12',
              '2','2','4','4','2','1','5','X','4','2','12','16','2','X','4','5',
              '4','X','5','5')

start <- c(247123880,91884413,88886155,9403011,40503634,10667741,88914884,
                      100632615,25804205,25803542,18925987,21501823,21501855,115902990,
                      26120955,22008406,432498,22008406,22008406,69306802,4144380,73083197,
                      47743372,34836043,16525257,315832,1558229,51048657,49635818,239952709,
                      69727769,27941625,80328938,49136485,49136654,96076105,133702693,315823,
                      16725215,69728318,88520557,89832606,202205081,124379013,16045662,89836880,
                      49657307,97117994,76547133,35051701,344973,1770075,49139874,77426085,
                      9406416,69727781,108238962,151006944,49121333,6669602,89419843,74214551,
                      91203955,115395615)

type <- c('Inversions','Deletions','Deletions','Deletions','Deletions','Duplications','Deletions','Deletions',
          'Duplications','Deletions','Duplications','Inversions','Inversions','Deletions','Duplications',
          'Deletions','Deletions','Deletions','Deletions','Inversions','Duplications','Inversions','Inversions',
          'Inversions','Deletions','Deletions','Deletions','Insertions','Deletions','Inversions','Inversions',
          'Inversions','Inversions','Deletions','Deletions','Inversions','Deletions','Deletions','Inversions',
          'Inversions','Deletions','Deletions','Deletions','Insertions','Inversions','Deletions','Deletions',
          'Deletions','Inversions','Deletions','Duplications','Inversions','Deletions','Deletions','Deletions',
          'Inversions','Deletions','Inversions','Deletions','Inversions','Inversions','Inversions','Deletions','Deletions')


per.found <- c(-0.040,0.080,0.080,0.040,0.080,0.040,0.080,0.040,0.040,0.120,0.040,-0.080,-0.080,0.040,0.040,0.120,
               0.040,0.120,0.120,-0.040,0.011,-0.011,-0.023,-0.023,0.011,0.023,0.011,0.011,0.011,-0.011,-0.034,
               -0.011,-0.023,0.011,0.011,-0.011,0.023,0.023,-0.023,-0.034,0.011,0.023,0.011,0.011,-0.023,0.023,
               0.011,0.011,-0.011,0.011,0.011,-0.023,0.011,0.057,0.011,-0.034,0.023,-0.011,0.011,-0.011,-0.023,
               -0.023,0.011,0.011)

df <- data.frame(chromosome = chr, start.coordinate = start, copy.number.type = type, per.found = per.found )

我想创建一个线图。我使用ggplot(构面)创建了一个图,但是问题是我无法连接两个构面之间的点。有没有办法做到这一点。如果有一种方法可以通过chromosome注释x轴比例,则不必使用多面。在下图中,虚线显示了我希望所有copy.number.type行具有的内容。

编辑:寻找简化的方法。

library(ggplot2)
ggplot(df, aes(x=start.coordinate,y=per.found, group=copy.number.type, color=copy.number.type))+
  geom_line()+
  geom_point()+
  facet_grid(.~chromosome,scales = "free_x", space = "free_x")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

所需的输出:如红色虚线所示。我想用虚线连接所有边界点。 enter image description here

1 个答案:

答案 0 :(得分:1)

注意:将染色体之间的线连接起来可能没有意义。

但这是避免刻面的一种方法:

library(dplyr)
df2 <- df %>% 
  mutate(chromosome = factor(chromosome, c(1, 2, 4, 5, 12, 16, 'X'))) %>% 
  arrange(chromosome, start.coordinate)
chromosome_positions <- df2 %>% 
  group_by(chromosome) %>% 
  summarise(start = first(start.coordinate), end = last(start.coordinate)) %>% 
  mutate(
    size = end - start,
    new_start = cumsum(lag(size, default = 0)),
    new_end = new_start + size
  )
df3 <- df2 %>% 
  left_join(chromosome_positions, 'chromosome') %>% 
  mutate(new_x = start.coordinate + (new_start - start))

ggplot(df3, aes(x=new_x,y=per.found, group=copy.number.type, color=copy.number.type))+
  geom_rect(
    aes(xmin = new_start, xmax = new_end, ymin = -Inf, ymax = Inf, fill = chromosome), 
    chromosome_positions, inherit.aes = FALSE, alpha = 0.3
  ) +
  geom_line() +
  geom_point() +
  geom_text(
    aes(x = new_start + 0.5 * size, y = Inf, label = chromosome),
    chromosome_positions, inherit.aes = FALSE, vjust = 1
  ) + 
  scale_fill_manual(values = rep(c('grey60', 'grey90'), 10), guide = 'none') +
  scale_x_continuous(expand = c(0, 0))

enter image description here