Setting dimensions of barplot area in R (for STRUCTURE-like plot)
0
0
Entering edit mode
9.3 years ago
devenvyas ▴ 760

I am trying to plot some STRUCTURE-like results from ADMIXTURE. I got some code from one of my adviser's collaborators to plot my results, but I am having problems.

I have noticed that R will randomly make certain individual bars double width and other bars zero width (i.e., some samples will fail to appear and simultaneously others will be double). There are 2033 individuals in the dataset from 161 populations (=160 separating bars). The double bars will often crossover population separating bars. I have been trying to tinker with the output resolution, but nothing seems to fix the issue.

Is there anyway to set the area of the barplot itself (excluding the axes and the margin area) and fix the width of the bars themselves? If each bar is locked to one pixel wide, but the barplot itself should only be 2193 pixels wide.

Since it is not my code, I don't want to post it, but here are few lines of code that are particularly relevant

Part of the structure.plot function definition

barplot(t(as.matrix(q)), space=0, border=NA, col=cols, axes=F, axisnames=F)

Final plot generation

tiff(file="structure-plot-3000.tiff", width=3000, height=500, antialias="default")
structure.plot(qdata, cex.axis=.8)

Thanks!

R • 6.0k views
ADD COMMENT
0
Entering edit mode

This sounds more of a StackOverflow question but if you add some more info like a snapshot of your data, in this case I believe it's "q", that can be used to reproduce your error/problem, you might be able to get some more help.

ADD REPLY
0
Entering edit mode

The input data form is as such

iid        Q1   Q2   Q13   Q14   pop       region       bigregion
Sample1    0    0    0     0     popname   regionname   bigregionname
Sample2    0    0    0     0     popname   regionname   bigregionname
Sample3    0    0    0     0     popname   regionname   bigregionname
Sample4    0    0    0     0     popname   regionname   bigregionname
Sample5    0    0    0     0     popname   regionname   bigregionname
Sample6    0    0    0     0     popname   regionname   bigregionname
Sample7    0    0    0     0     popname   regionname   bigregionname
Sample8    0    0    0     0     popname   regionname   bigregionname
Sample9    0    0    0     0     popname   regionname   bigregionname
Sample10   0    0    0     0     popname   regionname   bigregionname

(Q3-Q12 columns omitted, values are not all zero). Other parts of the code, sort the qdata input based on the order in another list and based on the magnitude of data values (within populations).

Here is the definition of the structure.plot function

structure.plot <- function(x, cex.axis=0.5) {
  qcols <- sapply(names(x), function(y) {length(grep("Q", y)) > 0})
  q <- x[,qcols]
  n <- ncol(q)
  #cols <- c("blue", "#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "red", "#A6761D", "#666666", "#E6AB02")[1:n]
  #cols <- brewer.pal(12, "Paired")
  #cols[11] <- "#999999"
  cols <- c("#6BC367","#BF59CB","#CC5735","#8BB5D0","#563C6A","#CDAF4F","#CA4A7D","#64322D","#5A6630","#CD9B9B","#7D7CCE","#8FD0AD","#455E5D","#9BDA3E")
  barplot(t(as.matrix(q)), space=0, border=NA, col=cols, axes=F, axisnames=F)

  ppos <- c()
  for (p in levels(x$pop)) {
    ppos <- c(ppos, mean(which(x$pop == p)))
  }
  axis(1, at=ppos, labels=levels(qdata$pop), tick=F, las=2, cex.axis=cex.axis)

  pmin <- c()
  for (p in levels(x$pop)) {
    pmin <- c(pmin, min(which(x$pop == p)))
  }
  pmin <- pmin - 0.5
  pmin <- pmin[pmin > 1]
  abline(v=pmin)
}
ADD REPLY
0
Entering edit mode

Anyone have any suggestions?

ADD REPLY

Login before adding your answer.

Traffic: 2530 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6