From b8e766031cf659c71bc53fb03228fc410d8773a0 Mon Sep 17 00:00:00 2001 From: Roy Francis Date: Wed, 14 Aug 2019 22:27:47 +0200 Subject: [PATCH] Added legendrow and legendspacing --- DESCRIPTION | 2 +- NEWS.md | 4 +++- R/plot.R | 9 ++++++++- R/qlist.R | 2 +- man/is.qlist.Rd | 6 ++++++ man/plotQ.Rd | 20 ++++++++++++-------- vignettes/index.Rmd | 4 ++-- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b89c634..82b391c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Type: Package Title: Tabulate, analyse and visualise admixture proportions from STRUCTURE, TESS, BAPS, ADMIXTURE and tab-delimited q-matrices files Version: 2.3.0 -Date: 2019-08-11 +Date: 2019-08-14 Author: Roy Mathew Francis Authors@R: person("Roy", "Francis", email = "roy.m.francis@outlook.com",role = c("aut", "cre")) Maintainer: Roy Mathew Francis diff --git a/NEWS.md b/NEWS.md index 5a41f22..2bc1932 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ # pophelper 2.3.0 * 11-Aug-2019 -* Built on R-3.6.0 +* Built on R-3.6.0. Min req R-3.5.0. * Removed CLUMPP and DISTRUCT executables * 'useexe' argument in ClumppExport() deprecated. * Added alignK() function for label switching within and between K @@ -13,6 +13,8 @@ * Added function sortQ() to sort runs in a qlist by a run attribute * Added function mergeQ() to merge replicate runs within K * Added function splitQ() to split a qlist object by an attribute +* 14-Aug-2019 +* Added arguments 'legendspacing' and 'legendrow' to plotQ() # pophelper 2.2.9 diff --git a/R/plot.R b/R/plot.R index e099c1e..7c8c80d 100644 --- a/R/plot.R +++ b/R/plot.R @@ -686,6 +686,8 @@ sortInd <- function(dframe=NULL,grplab=NA,selgrp=NA,ordergrp=FALSE,sortind=NA,gr #' to 4. #' @param legendtextsize A numeric indicating size of the legend text. Defaults #' to 3. +#' @param legendspacing Spacing between legend items. +#' @param legendrow Number of rows of legend. #' @param legendmargin A numeric vector of length 4 indicating top, right, #' bottom and left margins of the legend. #' @param barsize A numeric indicating the width of the bars. Defaults to 1. @@ -924,7 +926,7 @@ plotQ <- function(qlist=NULL,imgoutput="sep",clustercol=NA,sortind=NA,grplab=NA, pointsize=NA,pointcol="grey30",pointbgcol="grey30",pointtype="|",pointalpha=1, linepos=0.75,linesize=NA,linecol="grey30",linetype=1,linealpha=1, showdiv=TRUE,divgrp=NA,divcol="white",divtype="21",divsize=0.25,divalpha=1, - showlegend=FALSE,legendlab=NA,legendpos="right",legendkeysize=4,legendtextsize=3,legendmargin=c(0.5,0.5,0.5,0), + showlegend=FALSE,legendlab=NA,legendpos="right",legendkeysize=4,legendtextsize=3,legendspacing=2,legendrow=NULL,legendmargin=c(0.5,0.5,0.5,0), barsize=1,barbordersize=0,barbordercolour=NA, showyaxis=FALSE,showticks=FALSE,ticksize=0.1,ticklength=0.03, outputfilename=NA,imgtype="png",height=NA,width=NA,dpi=300,units="cm", @@ -1154,6 +1156,7 @@ plotQ <- function(qlist=NULL,imgoutput="sep",clustercol=NA,sortind=NA,grplab=NA, scale_x_continuous(breaks=df2$order_ind,labels=df2$ind,expand=c(0,0))+ scale_y_continuous(expand=c(0,0))+ scale_fill_manual(values=coll,labels=legendlab1)+ + guides(fill=guide_legend(nrow=legendrow,byrow=TRUE))+ facet_wrap(~run,labeller=labeller(run=facetnames),strip.position=sppos,scales="fixed",nrow=1)+ get(theme)(base_family=font,base_size=basesize) @@ -1163,6 +1166,7 @@ plotQ <- function(qlist=NULL,imgoutput="sep",clustercol=NA,sortind=NA,grplab=NA, legend.title=element_blank(), legend.key.size=grid::unit(legendkeysize,"points"), legend.text=element_text(size=legendtextsize,colour=indlabcol), + legend.spacing.x=grid::unit(legendspacing,"points"), legend.spacing=grid::unit(0,"points"), legend.justification=legendpos, legend.margin=margin(legendmargin[1],legendmargin[2],legendmargin[3],legendmargin[4],"points"), @@ -1543,6 +1547,7 @@ plotQ <- function(qlist=NULL,imgoutput="sep",clustercol=NA,sortind=NA,grplab=NA, scale_x_continuous(breaks=df3$order_ind,labels=df3$ind,expand=c(0,0))+ scale_y_continuous(expand=c(0,0))+ scale_fill_manual(values=coll,labels=legendlab1)+ + guides(fill=guide_legend(nrow=legendrow,byrow=TRUE))+ get(theme)(base_family=font,base_size=basesize) }else{ gg_plot_panel <- ggplot2::ggplot(data=df3,aes(x=order_cumulative,y=value,fill=variable))+ @@ -1550,6 +1555,7 @@ plotQ <- function(qlist=NULL,imgoutput="sep",clustercol=NA,sortind=NA,grplab=NA, scale_x_continuous(breaks=df3$order_cumulative,labels=df3$ind,expand=c(0,0))+ scale_y_continuous(expand=c(0,0))+ scale_fill_manual(values=coll,labels=legendlab1)+ + guides(fill=guide_legend(nrow=legendrow,byrow=TRUE))+ get(theme)(base_family=font,base_size=basesize) } @@ -1566,6 +1572,7 @@ plotQ <- function(qlist=NULL,imgoutput="sep",clustercol=NA,sortind=NA,grplab=NA, legend.title=element_blank(), legend.key.size=grid::unit(legendkeysize,"points"), legend.text=element_text(size=legendtextsize,colour=indlabcol), + legend.spacing.x=grid::unit(legendspacing,"points"), legend.spacing=grid::unit(0,"points"), legend.justification=legendpos, legend.margin=margin(legendmargin[1],legendmargin[2],legendmargin[3],legendmargin[4],"points"), diff --git a/R/qlist.R b/R/qlist.R index 3eafdaf..3b83ada 100644 --- a/R/qlist.R +++ b/R/qlist.R @@ -5,7 +5,7 @@ #' @param qlist A qlist object. #' @return Nothing. #' @details Returns error if the object is not a list, contents are not data.frames or NA list elements or missing run labels. Warnings are generated for non standard column labels or missing attributes 'ind' and 'k'. Warnings can be fixed by running \code{\link{as.qlist}}. -#' @example +#' @examples #' sfiles <- list.files(path=system.file("files/structure",package="pophelper"), #' full.names=TRUE) #' slist <- readQ(sfiles) diff --git a/man/is.qlist.Rd b/man/is.qlist.Rd index 3c67554..0a11540 100644 --- a/man/is.qlist.Rd +++ b/man/is.qlist.Rd @@ -18,6 +18,12 @@ Verify if a qlist is formatted correctly. \details{ Returns error if the object is not a list, contents are not data.frames or NA list elements or missing run labels. Warnings are generated for non standard column labels or missing attributes 'ind' and 'k'. Warnings can be fixed by running \code{\link{as.qlist}}. } +\examples{ +sfiles <- list.files(path=system.file("files/structure",package="pophelper"), +full.names=TRUE) +slist <- readQ(sfiles) +is.qlist(slist) +} \seealso{ \code{\link{as.list}} } diff --git a/man/plotQ.Rd b/man/plotQ.Rd index 6000774..40e5c94 100644 --- a/man/plotQ.Rd +++ b/man/plotQ.Rd @@ -29,14 +29,14 @@ plotQ(qlist = NULL, imgoutput = "sep", clustercol = NA, divgrp = NA, divcol = "white", divtype = "21", divsize = 0.25, divalpha = 1, showlegend = FALSE, legendlab = NA, legendpos = "right", legendkeysize = 4, legendtextsize = 3, - legendmargin = c(0.5, 0.5, 0.5, 0), barsize = 1, barbordersize = 0, - barbordercolour = NA, showyaxis = FALSE, showticks = FALSE, - ticksize = 0.1, ticklength = 0.03, outputfilename = NA, - imgtype = "png", height = NA, width = NA, dpi = 300, - units = "cm", theme = "theme_grey", basesize = 5, font = "", - na.rm = TRUE, quiet = FALSE, panelratio = c(3, 1), - exportplot = TRUE, returnplot = FALSE, returndata = FALSE, - exportpath = NULL) + legendspacing = 2, legendrow = NULL, legendmargin = c(0.5, 0.5, + 0.5, 0), barsize = 1, barbordersize = 0, barbordercolour = NA, + showyaxis = FALSE, showticks = FALSE, ticksize = 0.1, + ticklength = 0.03, outputfilename = NA, imgtype = "png", + height = NA, width = NA, dpi = 300, units = "cm", + theme = "theme_grey", basesize = 5, font = "", na.rm = TRUE, + quiet = FALSE, panelratio = c(3, 1), exportplot = TRUE, + returnplot = FALSE, returndata = FALSE, exportpath = NULL) } \arguments{ \item{qlist}{A qlist (list of dataframes). An output from \code{\link{readQ}}.} @@ -297,6 +297,10 @@ to 4.} \item{legendtextsize}{A numeric indicating size of the legend text. Defaults to 3.} +\item{legendspacing}{Spacing between legend items.} + +\item{legendrow}{Number of rows of legend.} + \item{legendmargin}{A numeric vector of length 4 indicating top, right, bottom and left margins of the legend.} diff --git a/vignettes/index.Rmd b/vignettes/index.Rmd index 78bef65..2bbc3f3 100644 --- a/vignettes/index.Rmd +++ b/vignettes/index.Rmd @@ -39,7 +39,7 @@ fc <- captioner::captioner(prefix="Fig. ") This vignette/tutorial aims to demonstrate the use of R package `pophelper`. This package contains functions that are useful for processing output results from programs used in the analysis of population structure such as ADMIXTURE^1^, FASTSTRUCTURE^6^, STRUCTURE^5^, TESS^3^, BAPS^8^ and any q-matrix output as a numeric tabular delimited file. -These softwares are popular programs used to discriminate populations, to determine population structure and to reveal the genetic composition of individuals using molecular markers. These programs generally use allelic frequency information to assign individuals to a pre-defined number of populations (*K*). The assignment is usually run for a range of *K* such as from *K*=2 to *K*=10. Multiple repeats are also usually carried out for each *K*. Each output file for each repeat of *K* showing the assignment probabilities of all individuals is referred to as the run file, cluster file or q-matrix file. `pophelper` has a set of functions that include reading into R, tabulating, summarising and plotting these run files. +These software are popular programs used to discriminate populations, to determine population structure and to reveal the genetic composition of individuals using molecular markers. These programs generally use allelic frequency information to assign individuals to a pre-defined number of populations (*K*). The assignment is usually run for a range of *K* such as from *K*=2 to *K*=10. Multiple repeats are also usually carried out for each *K*. Each output file for each repeat of *K* showing the assignment probabilities of all individuals is referred to as the run file, cluster file or q-matrix file. `pophelper` has a set of functions that include reading into R, tabulating, summarising and plotting these run files. This vignette covers the use of all important functions in the `pophelper` package. The demonstration is ordered in the manner of a typical workflow. Input and output codes are printed in a font different from body text like `this`. @@ -816,7 +816,7 @@ sapply(slist1[c(1,3)],ncol) # modified for this document p1 <- plotQ(slist1[1],returnplot=T,exportplot=F,quiet=T,basesize=11, showlegend=T, legendlab=c("group1","group2"), - legendkeysize=10,legendtextsize=10,legendmargin=c(2,2,2,0)) + legendkeysize=10,legendtextsize=10,legendmargin=c(2,2,2,0),legendrow=1) p2 <- plotQ(slist1[c(1,3)],imgoutput="join",returnplot=T,exportplot=F,quiet=T,basesize=11, showlegend=T, legendlab=c("group1","group2","group3"),