Reputation: 451
I have the following piece of code for a graph:
barplot(as.vector(t(mat1[1,3:ncol(mat1)])),las=2)
that I would like to alter so that the x-axis is replaced by the line y = 2; effectively moving the x-axis up 2 units as in the image below.
I need the bars to begin at 2 so that:
How can I make the column names of mat1
my x-axis categories?
Upvotes: 3
Views: 362
Reputation: 263311
Barplot always starts its bars at 0. Subtract 2 (or 5 as I did) from every y-value. Set ylim to range(y-values - 5). You will need to suppress plotting the y axis with yaxt="n". The xpd parameter to axis allows the range of labels to extend below the range of actual values.
set.seed(231)
tN <- table(Ni <- stats::rpois(100, lambda=5))
tNshift <- tN-5
barplot(tNshift, space = 1.5, yaxt="n", xaxt="n", ylim=range(tNshift))
abline(0,0)
axis(2, at= c(-5, pretty(tNshift)), labels=c(0, pretty(tNshift)+5), xpd=TRUE)
Upvotes: 7
Reputation: 7928
here is the first example from ?barplot
, slightly modified, with abline(x,y)
added
require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda=5))
barplot(tN, space = 1.5, axisnames=FALSE)
abline(5,0)
Sorry if this is not answering your specific questing, but I did not have any sample data to work from so I took the ?barplot
example.
Upvotes: 1