top of page

How to make a Barplot with Tess data and R

# Original Script by Benoit Simon-Bouhet (see http://sciences.univ-larochelle.fr/benoit-simon-bouhet ; contact: bsimonbo@univ-lr.fr)

# Creates a borplot from a data.frame containing one column with population names and n column with probabilities # col: vector of colors for the clusters # space: numeric. Spacing between bars of the barplot # horiz: logical. Should individual bars be plotted horizontally or vertically? # col.seg, lwd.seg: characteristics of segments between individual bars # col.pop, lwd.pop: characteristics of lines between populations # col.frame: color for the box surrounding the barplot # ...: other graphical parameters passed to par() structure.plot <- function(data, col=NULL, space=0, horiz=TRUE, col.seg=rgb(0.204,0.204,0.204,alpha=0.25), lwd.seg=.5, col.pop=1,lwd.pop=1.5, col.frame=1, axis=TRUE,...) {

nclust <- dim(data)[2] - 1 pst <- pos.tick(data[,1]) # Position of tick martks lab <- unique(data[,1]) # Label for each population

col <- if (is.null(col)) heat.colors(nclust) else col

if (horiz) { par(...) barplot(t(as.matrix(data[,-1])),col=col,border=FALSE,space=space,axes=F,names.arg=rep(NA,nrow(data)),horiz=horiz,...) segments(0,cumsum(table(data[,1])),1,cumsum(table(data[,1])),lwd=lwd.pop,col=col.pop) segments(0,1:(nrow(data)-1),1,1:(nrow(data)-1),lwd=lwd.seg,col=col.seg) rect(0,0,1,nrow(data),border=col.frame,lwd=lwd.pop) if (axis) axis(side=2,at=pst,labels=lab,las=1) } else { par(...) barplot(t(as.matrix(data[,-1])),col=col,border=FALSE,space=0,axes=F,names.arg=rep(NA,nrow(data)),horiz=horiz,...) segments(cumsum(table(data[,1])),0,cumsum(table(data[,1])),1,col=col.pop,lwd=lwd.pop) segments(1:(nrow(data)-1), 0, 1:(nrow(data)-1), 1, lwd=lwd.seg,col=col.seg) rect(0,0,nrow(data),1,border=col.frame,lwd=lwd.pop) if (axis) axis(side=1,at=pst,labels=lab) } }

# Internal function # pos.tick computes the position of tick marks for population labels on the x.axis # fac is a factor containing the population names for each individual pos.tick <- function(fac) { eff.pop <- table(fac) npop <- length(eff.pop) ps.tick <- numeric(npop) ps.tick[1] <- cumsum(eff.pop[1])/2 for (i in 2:npop) { ps.tick[i] <- max(cumsum(eff.pop[1:(i-1)])) + eff.pop[i]/2 } return(ps.tick) }

coord <- read.table("microsatcoord_PL + ML (Medit) 2.txt", row.names=1) coord <- coord[,1:2] ordered.coord <- coord[order(coord[,2]),]

proba <- read.table("ProbaRun29.txt", head=F) proba <- proba[,1:3] ordered.proba <- proba[order(coord[,2]),]

prob <- ordered.proba rownames(prob) <- rownames(ordered.coord) site <- rep(c("Galice","Gibraltar","Méditerrannée"), c(33, 39, 49)) prob$site <- site prob <- prob[,c(4,1:3)]

color <- c("antiquewhite2","cadetblue3","cadetblue4") par(mfrow=c(3,1)) structure.plot(prob, horiz = FALSE, col = color, axis = TRUE, main = "Barplot des individus en mer Méditerranée avec K = 3") axis(2, las=1)

# library(marmap)

out <- apply(prob[,-1], 1, which.max) pop <- factor(out, levels=1:3, labels=c("Galice","Méditerranée","Gibraltar")) table(pop) as.matrix(pop)


bottom of page