I used barplot() function to create a stacked chart from matrix.
Matrix looks like this:
1 0.989013 0.010987
2 0.999990 0.000010
3 0.999990 0.000010
4 0.999990 0.000010
code to create a stacked chart looks like this:
barplot(t(as.matrix(x)), col=c("cyan", "black"))
The problem is that I want to create custom x axis with ticks and labels at certain points. I used following to create x axis:
axis(1, at=keyindexes, labels=unique(t$V4), tck=-0.01)
where keyindexes is vector of bar numbers where I need to have ticks and unique(t$V4) is vector containing unique name for each tick.
Now the problem is that x axis does not match with chart area and is significantly shorter. Can anyone advice on how to make x axis longer?
Best Answer
The problem with axis labels is in fact that bars are not centered on whole numbers. For example, make some data with ten rows and plot them with
barplot()
. Then addaxis()
for x axis at numbers from 1 to 10.Now axis texts are not in right position.
To correct this problem,
barplot()
should be saved as object and this object should be used asat=
values in axis.EDIT - plotting only some ticks
If only some of axis ticks texts should be plotted then you can subset
m
values that are needed and provided the same length oflabels=
. In this example only 1., 5. and 10. bars are annotated.