简体   繁体   中英

Remove grid from label names in a combined graph with GGPLOT2

I produced a graph which is almost perfect for me. But i`m stuck on how to remove the grid from the annotated areas geom_text(). I use geom_text for plotting my values and for annotating the plots.

My graph looks like this: 在此输入图像描述

My data:

structure(list(Mash_pear = c(0.328239947270445, 0.752207607551684, 
0.812118104861163, 0.640824971449627, 0.615568052052443, 0.546635339103089, 
0.557460706464288, 0.650480192893698, 0.418044504894929, 0.52962586938499, 
0.727805116980878, 0.457751406173336, 0.429785015765779, 0.395830424689082, 
0.442222743260579, 0.296983456365718, 0.80883682959008, 0.81208596690244, 
0.710216015384062, 0.773625942359682), tRap_pear = c(0.0350096175177328, 
0.234255507711743, 0.23714999195134, 0.185536020521134, 0.191585098617356, 
0.201402054387186, 0.220911538536031, 0.216072802572045, 0.132247101763063, 
0.172753098431029, 0.280795579672784, 0.188134155907737, 0.217454200485912, 
0.0696965676717436, 0.233870791415384, 0.0544299934337156, 0.226678897883402, 
0.128806853122092, 0.117478493999246, 0.161184511733104), Beeml_pear = c(0.179209909971615, 
0.79129167285928, 0.856908302056589, 0.729078080521886, 0.709346164378725, 
0.669599784720647, 0.585348196746785, 0.639355942917055, 0.544909349368496, 
0.794652394149651, 0.863422091668162, 0.595508977637756, 0.533923256060852, 
0.843786110234149, 0.402095177866003, 0.293984533306754, 0.867067527220569, 
0.883565060756018, 0.727331622602897, 0.860485005688852), Mash_pear50 = c(0.192474082559755, 
0.679726904159742, 0.778564545349054, 0.573745352397321, 0.56633658385284, 
0.472559997318901, 0.462635414367878, 0.562128414492567, 0.354624921832056, 
0.64532681437697, 0.699503386816393, 0.384318378224603, 0.366299448984722, 
0.46938310547208, 0.282831366512569, 0.227004236832775, 0.754840213343737, 
0.763285417222461, 0.638421127411269, 0.719475286983103)), .Names = c("Mash_pear", 
"tRap_pear", "Beeml_pear", "Mash_pear50"), row.names = c("Aft1", 
"Alx3_3418.2", "Alx4_1744.1", "Arid3a_3875.1_v1_primary", "Arid3a_3875.1_v2_primary", 
"Arid3a_3875.2_v1_primary", "Arid3a_3875.2_v2_primary", "Arid5a_3770.2_v1_primary", 
"Arid5a_3770.2_v2_primary", "Aro80", "Arx_1738.2", "Ascl2_2654.2_v1_primary", 
"Ascl2_2654.2_v2_primary", "Asg1", "Atf1_3026.3_v1_primary", 
"Atf1_3026.3_v2_primary", "Bapx1_2343.1", "Barhl1_2590.2", "Barhl2_3868.1", 
"Barx1_2877.1"), class = "data.frame")

The code for producing the graph:

plotAll<-function(data,size=2, alpha=0.4){
  combs <- expand.grid(names(data), names(data))
  out <- do.call(rbind, apply(combs, 1, function(x) {
    tt <- data[, x]; names(tt) <- c("V1", "V2")
    tt <- cbind(tt, id1 = x[1], id2 = x[2])
  }))

  library(plyr)
  df.text=ddply(out[out$id1==out$id2,],.(id1,id2),summarise,
                pos=max(V1)-(max(V1)-min(V1))/2)
  out[out$id1==out$id2,c("V1","V2")]<-NA
  out$labels <- rownames(out)
  out$labels<-sapply(out$labels, function(x){
    strsplit(x, "_")[[1]][1]
  })

  fam<-read.table('input/genomic_info_subset.tsv', sep='\t')

  idx<-match(out$labels,fam$V1)
  newcol<-fam$V4[idx]
  newcol<-as.character(newcol)
  out$fam<-newcol
  require(ggplot2)
  ggplot(data = out, aes(x = V2, y = V1)) +
    geom_text(data = out[!is.na(out$V1),], aes(label = labels), size=size, alpha=alpha) +  geom_abline( slope=1 ) +
    facet_grid(id1 ~ id2, scales="fixed")+
    geom_text(data=df.text,aes(pos,pos,label=id1), size=15)  + 
    ggtitle("Corralation between measured & calculated affinities") +
    ylab("") + xlab("") + theme(strip.text=element_blank(), strip.background=element_blank()) +
    theme(plot.title=element_text(face="bold.italic",size="60", color="brown")) +
    theme(axis.title=element_text(face="bold.italic",size="40", color="darkorange4"))
    #return(out)
}

The thing that i want is to remove the slope line and the grid from the annotation plots in the middle eg Mash_pear, tRap_pear, Beeml_pear and Mash_pear50. I only want to keep the names and remove the slope and grid, so a nice white/ broken white background and only the text annotation. I tried to add theme, to the aes of 2nd geom_text out of my function but that doesn't work, also i tried to use annotate() instead of geom_text() but this gave me a lot of aes() errors and such.

You can use geom_rect() to add white rectangular above the line and grey background. Place this line between lines of facet_grid() and geom_text() . For this purpose you can use the same data frame df.text that is use to plot text labels.

+ geom_rect(data=df.text,aes(xmin=-Inf,xmax=Inf,ymin=-Inf,ymax=Inf),
        fill="white",inherit.aes=FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM