Commit 8d292bea authored by Philippe BORDRON's avatar Philippe BORDRON
Browse files

adapt size of text in legend of pca according to the number of groups

parent 63e0683b
......@@ -47,7 +47,7 @@ ACP<-function(d,transpose=T,scale=F,center=T) {
return(resacp);
}
acp2d<-function(pca, comp=1:2,group=NULL, plotVars = FALSE, pointSize=2, plotText=FALSE,fixedCoord=FALSE,main=NULL,ellipse=FALSE,color=NULL){
acp2d<-function(pca, comp=1:2, group=NULL, plotVars = FALSE, pointSize=2, plotText=FALSE, fixedCoord=FALSE, main=NULL, ellipse=FALSE, color=NULL){
if(!require("ggplot2")) stop("You must install ggplot2");
if(length(comp)!=2) stop("You must give a vector of 2 integer for comp parameter");
percentVar <- pca$percentVar
......@@ -59,11 +59,24 @@ acp2d<-function(pca, comp=1:2,group=NULL, plotVars = FALSE, pointSize=2, plotTex
graph<-ggplot(data=d, mapping = aes(x=PC1, y=PC2, label = rownames(d)))
}else{
d <- data.frame(PC1=pca[[coor]][,comp[1]], PC2=pca[[coor]][,comp[2]], group=group);
graph<-ggplot(data=d, mapping = aes(x=PC1, y=PC2,colour=group, label = rownames(d)))
graph<-ggplot(data=d, mapping = aes(x=PC1, y=PC2, colour=group, label = rownames(d)))
}
theme<-theme(panel.background = element_blank(),panel.border=element_rect(fill=NA),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),strip.background=element_blank(),axis.text.x=element_text(colour="black"),axis.text.y=element_text(colour="black"),axis.ticks=element_line(colour="black"),plot.margin=unit(c(1,1,1,1),"line"))
graph<-graph+theme
graph<-graph+functPlot(size=pointSize)+
legend_text_size <- 8
legend_text_size <- ifelse(is.null(group), legend_text_size, max(2,min(legend_text_size, round(legend_text_size*32/length(unique(group))))))
theme <- theme(panel.background = element_blank(),
panel.border=element_rect(fill=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background=element_blank(),
axis.text.x=element_text(colour="black"),
axis.text.y=element_text(colour="black"),
axis.ticks=element_line(colour="black"),
plot.margin=unit(c(1,1,1,1),"line"),
legend.text = element_text(size = legend_text_size) # manage legend text size according to number of conditions
)
graph <- graph + theme
graph <- graph + guides(colour = guide_legend(override.aes = list(size=legend_text_size/(2*pointSize)))) # adjust dot size into legend
graph <- graph + functPlot(size=pointSize)+
xlab(paste0("PC",comp[1],": ",round(percentVar[comp[1]] * 100),"% variance")) +
ylab(paste0("PC",comp[2],": ",round(percentVar[comp[2]] * 100),"% variance"))
if(fixedCoord)graph <- graph + coord_fixed(ratio=percentVar[comp[2]]/percentVar[comp[1]])
......@@ -96,7 +109,7 @@ plotDistrib<-function(data,type="boxplot",conditions=NULL,main=NULL,conditionNam
graph<-ggplot(data = tabGraph,mapping = aes_string("sample","val",color=conditionName))
}
graph<-graph+theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=.3))
graph<-graph+theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=.3))
if(type=="violin") graph<-graph+geom_violin()
if(type=="boxplot") graph<-graph+geom_boxplot()
if(! is.null(main)) graph<-graph+ggtitle(main)
......@@ -108,9 +121,20 @@ plotAllACP<-function(compo=4,acp,sampleAnnot,condCol,main="PCA"){
for(i in 1:(compo-1)){
for(j in (i+1):compo){
if(i == 1 & j ==2){
acp2d(acp,group = sampleAnnot[,condCol],plotText = TRUE,pointSize = 2,comp = c(i,j),main="PCA",fixedCoord = F)
acp2d(acp,
group = sampleAnnot[,condCol],
plotText = TRUE,
pointSize = 2,
comp = c(i,j),
main="PCA",
fixedCoord = F)
}
acp2d(acp,group = sampleAnnot[,condCol],pointSize = 2,comp = c(i,j),main="PCA",fixedCoord = F)
acp2d(acp,
group = sampleAnnot[,condCol],
pointSize = 2,
comp = c(i,j),
main="PCA",
fixedCoord = F)
#acp2d(acp,pointSize = 2,comp = c(i,j),plotVars = TRUE, plotText = TRUE,fixedCoord = F)
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment