adding legend forlogo plot

This commit is contained in:
Tanushree Tunstall 2022-01-14 10:18:01 +00:00
parent 426a5cb0b5
commit 3b7cea3c47
4 changed files with 60 additions and 55 deletions

View file

@ -32,13 +32,15 @@
#==================
# NOTE: my_logo_col
# Can be one of these: 'chemistry', 'hydrophobicity', 'clustalx', 'taylor'
# if 'chemistry' or 'hydrophobicity' --> then bg is grey with black font
# if 'clustalx'or 'taylor' --> then bg is black with white font
# if 'chemistry' or 'hydrophobicity' --> then bg is grey with black font (x, y and labels)
# if 'clustalx'or 'taylor' --> then bg is black with white font (x, y and labels)
#, theme_bgc =
#, xfont_bgc =
#, yfont_bgc =
#, xtt_col =
#, ytt_col =
# ADD legend for hydrophobicity
# ADD legend for hydrophobicity: done
# ADD option to remove empty positions
LogoPlotCustomH <- function(plotdf
@ -57,10 +59,12 @@ LogoPlotCustomH <- function(plotdf
#, yfont_bgc
, y_ats = 22
, y_tangle = 0
, x_tts = 25 # title size
, y_tts = 20
, leg_pos = "none"
#, leg_pos = c(0.8, 0.9)
, x_tts = 20 # title size
, y_tts = 23
#, xtt_col =
#, ytt_col =
, leg_pos = "none" # can be top, left, right and bottom or c(0.8, 0.9)
, leg_dir = "horizontal" #can be vertical or horizontal
, leg_ts = 15 # leg text size
, leg_tts = 16 # leg title size
)
@ -72,13 +76,8 @@ LogoPlotCustomH <- function(plotdf
logo_df = plotdf
if (y_axis_log){
log_colname = paste0("log_", y_axis_colname)
logo_df[log_colname] = log_value(logo_df[y_axis_colname])
}
logo_data_plot2 = logo_df[, c(x_axis_colname, symbol_colname, y_axis_colname)]
logo_data_plot2 = logo_df[, c(x_axis_colname, symbol_colname, y_axis_colname, log_colname)]
#==================
# logo data: OR
#==================
@ -99,21 +98,26 @@ LogoPlotCustomH <- function(plotdf
#==================
# logo data: logOR
#==================
logo_data_plot2_logor = logo_df[, c(x_axis_colname, symbol_colname, log_colname)]
wide_df2_logor = as.matrix(logo_data_plot2_logor %>% spread(x_axis_colname, log_colname, fill = 0.0))
class(wide_df2_logor)
rownames(wide_df2_logor) = wide_df2_logor[,1]
dim(wide_df2_logor)
#wide_df2_logor = subset(wide_df_logor, select = -c(1) )
wide_df2_logor = wide_df2_logor[,-1]
str(wide_df2_logor)
colnames(wide_df2_logor)
position_logor = as.numeric(colnames(wide_df2_logor))
if (y_axis_log){
log_colname = paste0("log_", y_axis_colname)
logo_df[log_colname] = log_value(logo_df[y_axis_colname])
logo_data_plot2 = logo_df[, c(x_axis_colname, symbol_colname, y_axis_colname,log_colname )]
logo_data_plot2_logor = logo_df[, c(x_axis_colname, symbol_colname, log_colname)]
wide_df2_logor = as.matrix(logo_data_plot2_logor %>% spread(x_axis_colname, log_colname, fill = 0.0))
class(wide_df2_logor)
rownames(wide_df2_logor) = wide_df2_logor[,1]
dim(wide_df2_logor)
wide_df2_logor = wide_df2_logor[,-1]
str(wide_df2_logor)
colnames(wide_df2_logor)
position_logor = as.numeric(colnames(wide_df2_logor))
}
######################################
# Generating plots with given y_axis
@ -123,10 +127,11 @@ LogoPlotCustomH <- function(plotdf
if (my_logo_col %in% c('clustalx','taylor')) {
cat("\nSelected colour scheme:", my_logo_col
, "\nUsing black theme\n")
theme_bgc = "black"
theme_bgc = "black"
xfont_bgc = "white"
yfont_bgc = "black"
yfont_bgc = "white"
xtt_col = "white"
ytt_col = "white"
}
if (my_logo_col %in% c('chemistry', 'hydrophobicity')) {
@ -135,7 +140,8 @@ LogoPlotCustomH <- function(plotdf
theme_bgc = "grey"
xfont_bgc = "black"
yfont_bgc = "black"
xtt_col = "black"
ytt_col = "black"
}
LogoPlot = ggseqlogo(wide_df2_or
@ -152,10 +158,12 @@ LogoPlotCustomH <- function(plotdf
, hjust = 1
, vjust = 0
, colour = yfont_bgc)
, axis.title.y = element_text(size = x_tts)
, axis.title.x = element_text(size = y_tts)
#, legend.position = "bottom") +
, axis.title.x = element_text(size = x_tts
, colour = xtt_col)
, axis.title.y = element_text(size = y_tts
, colour = ytt_col)
, legend.position = leg_pos
, legend.direction = leg_dir
, plot.background = element_rect(fill = theme_bgc))+
#, legend.text = element_text(size = leg_ts)
#, legend.title = element_text(size = leg_tts))+
@ -165,7 +173,6 @@ LogoPlotCustomH <- function(plotdf
, limits = factor(1:length(position_or))) +
ylab(y_lab)
#print(logo_or)
return(LogoPlot)
}

View file

@ -7,21 +7,20 @@ LogoPlotCustomH (plotdf = merged_df3
, x_axis_colname = "position"
, y_axis_colname = "or_mychisq"
, symbol_colname = "mutant_type"
, y_axis_log = T
, y_axis_log = F
, log_value = log10
, my_logo_col = 'hydrophobicity'
#, theme_bgc
# , xfont_bgc
# , yfont_bgc
, my_logo_col = 'chemistry'
, x_lab = "Position"
, y_lab = "Odds Ratio"
, x_ats = 12 # text size
, x_tangle = 90 # text angle
, y_ats = 22
, y_tangle = 0
, x_tts = 25 # title size
, y_tts = 20
, leg_pos = "none"
, x_tts = 20 # title size
, y_tts = 23
#, leg_pos = c(0.05,-0.12)
, leg_pos = "top"
, leg_dir = "horizontal"
, leg_ts = 15 # leg text size
, leg_tts = 16 # leg title size
)