|
| 1 | +#' @eval get_description('plsda_vip_summary_plot') |
| 2 | +#' @export plsda_vip_summary_plot |
| 3 | +#' @include PLSDA_class.R |
| 4 | +#' @examples |
| 5 | +#' D = iris_DatasetExperiment() |
| 6 | +#' M = mean_centre()+PLSDA(factor_name='Species') |
| 7 | +#' M = model_apply(M,D) |
| 8 | +#' |
| 9 | +#' C = plsda_vip_summary_plot(n_features=30) |
| 10 | +#' chart_plot(C,M[2]) |
| 11 | +plsda_vip_summary_plot = function(n_features=50,...) { |
| 12 | + out=struct::new_struct('plsda_vip_summary_plot', |
| 13 | + n_features=n_features, |
| 14 | + ...) |
| 15 | + return(out) |
| 16 | +} |
| 17 | + |
| 18 | +.plsda_vip_summary_plot<-setClass( |
| 19 | + "plsda_vip_summary_plot", |
| 20 | + contains=c('chart'), |
| 21 | + slots=c( |
| 22 | + n_features='entity' |
| 23 | + ), |
| 24 | + prototype = list( |
| 25 | + name='PLSDA VIP summary plot', |
| 26 | + description='A plot of the Variable Importance for Projection (VIP) scores for a PLSDA model for the top selected features.', |
| 27 | + type="chart", |
| 28 | + libraries=c('pls','ggplot2','reshape2','cowplot'), |
| 29 | + .params=c('n_features'), |
| 30 | + |
| 31 | + n_features = entity( |
| 32 | + name = 'Number of features', |
| 33 | + description = 'The number of features to include in the summary.', |
| 34 | + type=c('numeric','integer'), |
| 35 | + value=50, |
| 36 | + max_length=1 |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | +) |
| 41 | + |
| 42 | +#' @export |
| 43 | +#' @template chart_plot |
| 44 | +setMethod(f="chart_plot", |
| 45 | + signature=c("plsda_vip_summary_plot",'PLSDA'), |
| 46 | + definition=function(obj,dobj) { |
| 47 | + |
| 48 | + # max vip |
| 49 | + data=dobj$vip |
| 50 | + |
| 51 | + # max sure we dont over the max number of features |
| 52 | + obj$n_features=min(nrow(data),obj$n_features) |
| 53 | + |
| 54 | + max_vip=as.data.frame(apply(data,1,max)) |
| 55 | + data$feature_id=rownames(data) |
| 56 | + data$max=max_vip[,1] |
| 57 | + |
| 58 | + # sort by max vip |
| 59 | + vip_order=order(-data$max) |
| 60 | + data=data[vip_order,] |
| 61 | + |
| 62 | + data2=reshape2::melt(data[1:obj$n_features,seq_len(ncol(data)-1)],id.vars = 'feature_id') |
| 63 | + data2$max=rep(data$max[1:obj$n_features],ncol(data)-2) |
| 64 | + |
| 65 | + g1 = ggplot(data=data[1:obj$n_features,],aes_string(x='max',y='reorder(feature_id,max)')) + |
| 66 | + geom_point() + |
| 67 | + labs(x="VIP score", |
| 68 | + y="Feature") + |
| 69 | + theme_Publication() + |
| 70 | + theme( |
| 71 | + panel.background = element_blank(), |
| 72 | + panel.grid.major = element_line(colour="#f0f0f0") |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | + g2 = ggplot(data=data2[],aes_string(x='variable',y='reorder(feature_id,max)')) + |
| 77 | + geom_tile(aes_string(fill='value'),colour = "black",width=0.8,height=0.8) + |
| 78 | + scale_fill_gradient2(low='#5e4fa2',mid='#ffffbf',high='#9e0142', |
| 79 | + midpoint=(quantile(data2$value,0.05)+quantile(data2$value,0.95))/2, |
| 80 | + limits=c(quantile(data2$value,0.05),quantile(data2$value,0.95)),oob=scales::squish,name='VIP score') + |
| 81 | + theme_Publication() + |
| 82 | + theme(axis.title.y=element_blank(), |
| 83 | + axis.text.y=element_blank()) + |
| 84 | + coord_fixed()+ |
| 85 | + theme(axis.text.x = element_text(angle = 45,hjust=-0.5),axis.title.x = element_blank()) + |
| 86 | + scale_x_discrete(position = "top") + |
| 87 | + theme(legend.position='right',legend.direction = 'vertical') |
| 88 | + |
| 89 | + |
| 90 | + G=cowplot::plot_grid(g1,g2,nrow=1,align = 'h',axis='tb') |
| 91 | + |
| 92 | + return(G) |
| 93 | + } |
| 94 | +) |
0 commit comments