1

When I plot a stacked barplot in R, the bars are placed with equal gaps in between and the x axis values are just used as labels. I want to have the bars placed closer or farther apart based on the x axis values. Can someone help me get this plot using R?

Edit:

# data.frame newtest
      A   B   C   D
100  0.2 0.3 0.1 0.4
400  0.3 0.5 0.1 0.1
500  0.1 0.3 0.4 0.2
600  0.4 0.2 0.2 0.2
1000 0.1 0.5 0.1 0.3
1500 0.3 0.2 0.2 0.3
1600 0.4 0.1 0.3 0.2
1700 0.1 0.1 0.7 0.1
2500 0.3 0.2 0.1 0.4

# plot
barplot(t(as.matrix(newtest)), col = c("cyan", "lightblue", "yellow", "green"), 
          legend = colnames(newtest), cex.main = 0.5, cex.axis = 0.5, 
          cex.lab = 0.5, lwd = 0.02)

Here's the plot: barplot

The bars are just labelled as per the row names. But I want the bars of 400,500,600 to be closer to each other, empty space to represent no blocks for 700,800,900, then the bar for 1000, then empty space till 1500, bars of 1500, 1600, 1700, etc.

Arun
  • 116,683
  • 26
  • 284
  • 387
Ganesh
  • 126
  • 5
  • Hi there! Please make your post reproducible by having a look at [**How to make a great reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for us to help you. Thank you. – Arun Mar 21 '13 at 08:17
  • 1
    Thanks Arun. I have added sample data and the code I used for plotting. I am not able to post the image of the plot I got. – Ganesh Mar 21 '13 at 09:01
  • I've edited your post, added the plot and edited the tag. This is not a ggplot. – Arun Mar 21 '13 at 10:18

1 Answers1

2

Given that the x axis in a barplot represents a categorical variable, I don't think there's a solution other than introducing extra dummy observations in your data:

extracolnames <- setdiff(seq(100,2500,by=100) ,rownames(newtest))
extracols <- replicate(length(extracolnames), rep(0,4))
colnames(extracols) <- extracolnames
dat <- cbind(t(as.matrix(newtest)), extracols)
dat <- dat[,order(as.numeric(colnames(dat)))]
barplot(dat, col=c("cyan","lightblue","yellow","green"), legend=colnames(newtest), cex.main=0.5, cex.axis=0.5, cex.lab=0.5, lwd=0.02)
Theodore Lytras
  • 3,955
  • 1
  • 18
  • 25