How can I change the order and name of the facet_grid in ggplot?

时间:2018-04-18 17:54:07

标签: r

I have three tables below:

table1 <- structure(list(Pos = 1:18, A = c(16.8508287292818, 0, 0.552486187845304, 
0, 1.10497237569061, 1.38121546961326, 2.76243093922652, 1.10497237569061, 
1.10497237569061, 0, 1.38121546961326, 1.10497237569061, 1.10497237569061, 
0.552486187845304, 0.276243093922652, 1.38121546961326, 0.552486187845304, 
18.232044198895), C = c(1.93370165745856, 0.276243093922652, 
0.828729281767956, 0.276243093922652, 0, 0.552486187845304, 0, 
0.552486187845304, 0, 0.276243093922652, 0.276243093922652, 0, 
0.828729281767956, 1.38121546961326, 0, 0.276243093922652, 0.828729281767956, 
4.97237569060773), G = c(1.10497237569061, 2.48618784530387, 
0.276243093922652, 0.828729281767956, 0.276243093922652, 0, 0, 
1.65745856353591, 0.276243093922652, 0, 0.276243093922652, 1.10497237569061, 
0.552486187845304, 0.828729281767956, 0.828729281767956, 0.276243093922652, 
0, 3.03867403314917), T = c(0.828729281767956, 0, 0.828729281767956, 
1.10497237569061, 0, 0, 0, 0.552486187845304, 0, 0.828729281767956, 
1.65745856353591, 0.276243093922652, 1.10497237569061, 0.552486187845304, 
0, 0.552486187845304, 0.552486187845304, 14.6408839779006)), .Names = c("Pos", 
"A", "C", "G", "T"), class = "data.frame", row.names = c(NA, 
-18L))


table10 <- structure(list(Pos = 1:20, A = c(0.634062678654956, 0.956291253053376, 
0.306636869185593, 0.363806454965958, 0.197494932695806, 0.129930876773556, 
0.11433917156073, 0.20788940283769, 0.332623044540305, 1.11220830518164, 
2.73894288238657, 0.748401850215685, 0.239072813263344, 1.21615300660049, 
0.20788940283769, 0.446962216101034, 0.171508757341095, 0.145522581986383, 
1.03944701418845, 21.0695909775999), C = c(0.457356686242919, 
0.441764981030092, 0.384595395249727, 0.265058988618055, 0.301439634114651, 
0.254664518476171, 0.524920742165168, 1.35128111844499, 0.665246089080609, 
1.13299724546541, 0.779585260641339, 0.327425809469362, 0.410581570604438, 
0.3690036900369, 0.394989865391612, 0.405384335533496, 0.618470973442129, 
0.623668208513071, 0.608076503300244, 2.59342030040019), G = c(1.13819448053635, 
0.670443324151551, 1.29930876773556, 0.841952081492646, 1.36687282365781, 
0.717218439790032, 1.35647835351593, 1.23174471181332, 1.35647835351593, 
0.493737331739515, 0.389792630320669, 0.618470973442129, 0.358609219895016, 
0.545709682448937, 0.945896782911491, 0.332623044540305, 1.6475235174887, 
1.86580739046827, 0.763993555428512, 4.99454290317551), T = c(0.343017514682189, 
0.48334286159763, 0.155917052128268, 0.181903227482979, 0.291045163972767, 
0.280650693830882, 0.16111428719921, 0.124733641702614, 0.176705992412037, 
0.150719817057326, 0.353411984824074, 0.166311522270152, 0.187100462553921, 
0.135128111844499, 0.109141936489787, 0.150719817057326, 0.192297697624864, 
0.150719817057326, 1.08102489475599, 26.9008887271971)), .Names = c("Pos", 
"A", "C", "G", "T"), class = "data.frame", row.names = c(NA, 
-20L))

table11 <- structure(list(Pos = 1:21, A = c(0.654134666904804, 0.314788355063404, 
0.401857474549027, 0.303625647437042, 0.303625647437042, 0.154045365243794, 
0.174138238971245, 0.185300946597607, 0.352741560993034, 0.893016610108948, 
1.09841043043401, 0.580460796570816, 0.234416860153599, 12.0423289873192, 
0.225486694052509, 0.350509019467762, 0.388462225397392, 0.145115199142704, 
0.256742275406323, 0.919807108412216, 17.1347562064654), C = c(0.464368637256653, 
0.692087872834435, 0.379532059296303, 0.370601893195213, 0.397392391498482, 
0.232184318628327, 0.370601893195213, 1.11850330416146, 0.727808537238793, 
0.986783354170388, 0.85729594570459, 0.60278621182354, 0.408555099124844, 
0.328183604215038, 0.359439185568852, 0.328183604215038, 0.506786926236828, 
0.370601893195213, 0.636274334702626, 0.453205929630291, 2.62100375066976
), G = c(1.00241114484729, 0.375066976245758, 1.38864082871941, 
0.370601893195213, 1.28817646008216, 0.879621360957314, 1.36185033041615, 
2.20128594391856, 0.743436327915699, 0.395159849973209, 0.562600464368637, 
0.841668155027684, 0.703250580460797, 0.444275763529202, 0.872923736381497, 
0.337113770316128, 1.86193963207716, 1.15199142704054, 0.551437756742275, 
2.34193606001072, 5.12368280050009), T = c(0.361671727094124, 
0.332648687265583, 0.1696731559207, 0.20762636185033, 0.183068405072334, 
0.151812823718521, 0.129487408465797, 0.136185033041615, 0.221021611001965, 
0.165208072870155, 0.171905697445973, 0.107161993213074, 0.149580282193249, 
0.136185033041615, 0.107161993213074, 0.138417574566887, 0.223254152527237, 
0.127254866940525, 0.145115199142704, 0.899714234684765, 20.6108233613145
)), .Names = c("Pos", "A", "C", "G", "T"), class = "data.frame", row.names = c(NA, 
-21L))

I now want to plot the histogram for the A, C, G, T columns as below:

    p <- bind_rows(table1,table10, table11, .id = "table") %>%
              gather(Base, Percent, -Pos, -table) %>%
              ggplot(aes(Pos, Percent)) +
              geom_col(aes(fill = Base), position = position_dodge()) +
              facet_grid(table~.)

  p <- p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                 panel.background = element_blank(), axis.line = element_line(colour = "black"))+
    scale_x_continuous(breaks=c(seq(1, 24, 3), 24))

  p <- p+labs(x="Mismatch position", y="Percent mismatch")
  p

My code only creates plots where facet_grids are labelled 1,2 and 3 for table1, table10 and table11, respectively. I want to change the facets as name_first for table1, name_tenth for table10 andname_eleventh` for table11. Any help would be appreciated!

1 个答案:

答案 0 :(得分:0)

作为Gregor评论的替代方案,您可以使用字符向量通过传递到as_labeller内的facet_grid

来标注构面。
correct_names <- c("1" = "name_first", "2" = "name_tenth", "3" = 
"name_eleventh")

p <- bind_rows(table1,table10, table11, .id = "table") %>%
  gather(Base, Percent, -Pos, -table) %>%
  ggplot(aes(Pos, Percent)) +
  geom_col(aes(fill = Base), position = position_dodge()) +
  facet_grid(table~., labeller = as_labeller(correct_names))

p <- p + theme(panel.grid.major = element_blank(), panel.grid.minor = 
element_blank(),
           panel.background = element_blank(), axis.line = 
element_line(colour = "black"))+
  scale_x_continuous(breaks=c(seq(1, 24, 3), 24))

p <- p+labs(x="Mismatch position", y="Percent mismatch")
p

enter image description here

将来,请记住检查您的代码是否可重现。我不得不做

library(dplyr)
library(tidyr)
library(ggplot2)

为了运行您的代码