一个图中有多个直方图

时间:2018-04-17 22:39:22

标签: r

我至少有5个表,如下所示(table1和table2)。我想为每个位置(Pos)列绘制A,T,G,C列的直方图。我想在一个情节中绘制两个表格。我怎么能在R中这样做?

table1<- structure(list(Pos = 1:19, A = c(48L, 12L, 9L, 8L, 26L, 34L, 
    18L, 3L, 3L, 10L, 8L, 7L, 12L, 4L, 9L, 7L, 9L, 7L, 243L), C = c(13L, 
    2L, 2L, 6L, 2L, 2L, 11L, 6L, 2L, 2L, 10L, 6L, 10L, 4L, 1L, 6L, 
    9L, 1L, 84L), G = c(12L, 4L, 1L, 5L, 6L, 1L, 4L, 7L, 8L, 7L, 
    7L, 1L, 8L, 5L, 3L, 6L, 3L, 2L, 45L), T = c(32L, 7L, 3L, 8L, 
    4L, 3L, 5L, 4L, 5L, 12L, 16L, 1L, 9L, 3L, 2L, 9L, 1L, 14L, 226L
    )), .Names = c("Pos", "A", "C", "G", "T"), class = "data.frame", row.names = c(NA, 
    -19L))

table2<- structure(list(Pos = 1:19, A = c(48L, 12L, 9L, 8L, 26L, 34L, 
18L, 3L, 3L, 10L, 8L, 7L, 12L, 4L, 9L, 7L, 9L, 7L, 243L), C = c(13L, 
2L, 2L, 6L, 2L, 2L, 11L, 6L, 2L, 2L, 10L, 6L, 10L, 4L, 1L, 6L, 
9L, 1L, 84L), G = c(12L, 4L, 1L, 5L, 6L, 1L, 4L, 7L, 8L, 7L, 
7L, 1L, 8L, 5L, 3L, 6L, 3L, 2L, 45L), T = c(32L, 7L, 3L, 8L, 
4L, 3L, 5L, 4L, 5L, 12L, 16L, 1L, 9L, 3L, 2L, 9L, 1L, 14L, 226L
)), .Names = c("Pos", "A", "C", "G", "T"), class = "data.frame", row.names = c(NA, 
-19L))

1 个答案:

答案 0 :(得分:2)

首先,组合表并添加一列以使用dplyr::bind_rows标识表。然后你可以在table上面对。

对于并排图,请改用+ facet_grid(~table)

library(tidyverse)
bind_rows(table1, table2, .id = "table") %>%  
  gather(base, value, -Pos, -table) %>% 
  ggplot(aes(Pos, value)) + 
    geom_col(aes(fill = base), position = position_dodge()) +
    facet_grid(table~.)

enter image description here