Setting up charts in R

  • I have the following piece of code for the graph: barplot(as.vector(t(mat1[1,3:ncol(mat1)])),las=2)

    what I would like to change so that the x-axis is replaced by the line y = 2; effectively moving the x-axis up 2 units as shown in the image below. enter image description here

    I need bars to start with 2 so that:

    • the line with the value 3 starts at the line y = 2 and goes up to the end at y = 3.
    • line with value 0 starts at line y = 2 and goes down to the end at y = 0
  • How can I create the column names of mat1

    my x-axis categories?

+3


source to share


2 answers


Barplot always starts its bars at 0. Subtract 2 (or 5 as I did) from each y-value. Set ylim to range (y values ​​are 5). You will need to suppress the plotting of the y axis with yaxt = "n". The xpd parameter for the axis allows the label range to expand below the range of valid 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

+7


source


here is the first example from ?barplot

, slightly modified, with the 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 doesn't answer your specific quest, but I didn't have any sample data to work with, so I took an example ?barplot

.

+1


source







All Articles