Commit 208c8b30 authored by Philippe BORDRON's avatar Philippe BORDRON
Browse files

reduce the number of columns in legend in order to maximize the plot surface

parent f27f3ab7
......@@ -53,16 +53,17 @@ acp2d<-function(pca, comp=1:2, group=NULL, plotVars = FALSE, pointSize=2, plotTe
percentVar <- pca$percentVar
functPlot<-ifelse(plotText,geom_text,geom_point)
coor=ifelse(plotVars,"rotation","x")
ngroup <- 0
if(is.null(group)){
d <- data.frame(PC1=pca[[coor]][,comp[1]], PC2=pca[[coor]][,comp[2]]);
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)))
ngroup <- length(unique(group))
}
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))))))
default_legend_text_size <- 8
legend_text_size <- ifelse(is.null(group), default_legend_text_size, max(2,min(default_legend_text_size, round(default_legend_text_size*32/ngroup))))
theme <- theme(panel.background = element_blank(),
panel.border=element_rect(fill=NA),
panel.grid.major = element_blank(),
......@@ -75,7 +76,11 @@ acp2d<-function(pca, comp=1:2, group=NULL, plotVars = FALSE, pointSize=2, plotTe
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 + guides(col = guide_legend(
ncol = ifelse(ngroup == 0, NULL, ceiling(ngroup/24)), # limit the number of columns to keep the plot large enough; maybe take legend_text_size
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"))
......
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