user1202664
user1202664

Reputation: 451

Customising graphs in R

  1. 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. enter image description here

    I need the bars to begin at 2 so that:

    • a bar with a value of 3 begins from the y=2 line and ascends to end at y=3.
    • a bar with a value of 0 begins from the y=2 line and descends to end at y=0
  2. How can I make the column names of mat1 my x-axis categories?

Upvotes: 3

Views: 362

Answers (2)

IRTFM
IRTFM

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)

enter image description here

Upvotes: 7

Eric Fail
Eric Fail

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)

enter image description here

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

Related Questions