consurf_cols = consurf_palette1 consurf_cols = consurf_palette2 #consurf_cols = myCOL consurf_col_labs = c("Insufficient Data" , "Variable" , "2", "3", "4", "5" , "6", "7", "8", "Conserved") #breaks <- levels(as.factor(mtcars$cyl)) #colours <- ifelse(breaks == 4, "red", "blue") aa_position_colname = "position" length(unique(merged_df3[[aa_position_colname]])) positionF <- levels(as.factor(merged_df3[[aa_position_colname]])) length(positionF) aa_pos_colours = ifelse(positionF%in%aa_pos_sry, "purple" , ifelse(positionF%in%aa_pos_rna, "orange", "black" )) bar = merged_df3 aa_colour_colname = "lab_bg" bar[[aa_colour_colname]] = ifelse(bar[[aa_position_colname]]%in%aa_pos_sry , "purple" , ifelse(bar[[aa_position_colname]]%in%aa_pos_rna , "orange", "white" )) head(bar[[aa_position_colname]]) head(bar[[aa_colour_colname]]) ###################################################################### my_xlim = length(unique(bar[[aa_position_colname]])); my_xlim ymin = min(bar$consurf_score) ymax = max(bar$consurf_score) g = ggplot(bar, aes(x = factor(position) , y = consurf_score , colour = factor(consurf_colour_rev))) + coord_cartesian(xlim = c(1, my_xlim) , ylim = c(ymin, ymax) , clip = "off") + geom_point() + scale_colour_manual( values = consurf_cols , labels = consurf_col_labs ) + geom_errorbar(aes(ymin = consurf_ci_lower, ymax = consurf_ci_upper)) # g0 = g + geom_tile(aes(,-2, width = 0.95, height = -0.2) # #, fill = bar$lab_bg # , fill = bar[[aa_colour_colname]] # , colour = "white") g0 = g + geom_tile(aes(,-1.7, width = 0.95, height = 0.3) , fill = bar$ligD_colours , colour = "black") # g0 = g + # geom_tile(aes(,-1.9, width = 0.95, height = 0.2) # , fill = bar[[aa_colour_colname4]] # , colour = "white") + # geom_tile(aes(,-2, width = 0.95, height = 0.3) # , fill = bar[[aa_colour_colname3]] # , colour = "white" # )+ # geom_tile(aes(,-2.1, width = 0.95, height = 0.35) # , fill = bar[[aa_colour_colname2]] # , colour = "white" # )+ # geom_tile(aes(,-2.2, width = 0.95, height = 0.45) # , fill = bar[[aa_colour_colname1]] # , colour = "white" # ) g1 = g + theme( axis.text.x = element_text(size = 10 , angle = 90 , hjust = 1 , vjust = 0.4 , face = "bold" #, colour = aa_pos_colours ) , axis.text.y = element_text(size = 12 , angle = 0 , hjust = 1 , vjust = 0) , axis.title.x = element_text(size = 12) , axis.title.y = element_text(size =12 ) , panel.background = element_rect(fill = "black", color = "black") , panel.grid.major = element_line(color = "black") , panel.grid.minor = element_line(color = "black")) + guides(colour = guide_legend(title = "Consurf" , position = "top" #, direction = "horizontal" )) + labs(title = "" #, x = "Wild-type position" , x = "" , y = "Consurf score") g1 # add tile afterwards g2 = g1 + geom_tile(aes(,-1.7, width = 0.95, height = 0.3) , fill = bar$ligD_colours , colour = "black") g2 g3 = g2 + geom_tile(aes(,-1.9, width = 0.95, height = 0.3) , fill = bar[[aa_colour_colname4]] , colour = "white") + geom_tile(aes(,-2, width = 0.95, height = 0.3) , fill = bar[[aa_colour_colname3]] , colour = "white" )+ geom_tile(aes(,-2.1, width = 0.95, height = 0.35) , fill = bar[[aa_colour_colname2]] , colour = "white" )+ geom_tile(aes(,-2.2, width = 0.95, height = 0.35) , fill = bar[[aa_colour_colname1]] , colour = "white" ) g3 #============================================== #Multiple legends in a plot with geom_tile #============================================== # https://stackoverflow.com/questions/24822621/multiple-legends-in-a-plot-with-geom-tile g_legend<-function(a.gplot){ tmp <- ggplot_gtable(ggplot_build(a.gplot)) leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend <- tmp$grobs[[leg]] legend } legend1 = g_legend(g1) g2 = g1 + geom_tile(aes(,-1.7, width = 0.95, height = 0.3) , fill = bar$ligD_colours , colour = "black") g2 legend2 = g_legend(g2) grid.arrange(g1+theme(legend.position = 'none') #, legend1 , legend2 , ncol = 2 , widths=c(4/6, 1/6)) ############################################################### ############################################################### lig_min = min(round(bar[[LigDist_colname]])); lig_min lig_max = max(round(bar[[LigDist_colname]])); lig_max lig_mean = round(mean(bar[[LigDist_colname]])); lig_mean labels = seq(lig_min, lig_max, len = 5); labels labelsD = round(labels, digits = 0); labelsD g = ggplot(bar, aes( x = factor(.data[[aa_position_colname]]) , y = .data[[LigDist_colname]])) g # yayy g1 = g + geom_tile(aes(fill = .data[[LigDist_colname]]) , colour = "white") + #scale_fill_gradient(low = "green", high = "red") scale_fill_gradient2(midpoint = lig_mean , low = "green" , mid = "yellow" , high = "red" , breaks = labels #, n.breaks = 11 #, minor_breaks = c(2, 4, 6, 8, 10) , limits = c(lig_min, lig_max) , labels = labelsD , name = "Ligand Distance") g1 main_leg = g_legend(g2) main_leg ligD_leg = g_legend(g1) grid.arrange(ligD_leg , ncol = 2 , widths=c(4/6, 1/6)) grid.arrange(g2+theme(legend.position = 'none') , main_leg , ligD_leg , ncol = 3 , widths=c(9/10, 0.5/10, 0.5/10)) ########################## g3 = g2 + theme(legend.position = 'none') g3 g4 = g3 + geom_tile(aes(fill = .data[['consurf_score']]) , colour = "white") g4 + geom_tile(scale_fill_gradient(consurf_palette2) midpoint = lig_mean , low = "green" , mid = "yellow" , high = "red" , breaks = labels #, n.breaks = 11 #, minor_breaks = c(2, 4, 6, 8, 10) , limits = c(lig_min, lig_max) , labels = labelsD , name = "Ligand Distance") g1