Home > Software engineering >  Remove grey zero reference line in R lattice barchart
Remove grey zero reference line in R lattice barchart

Time:12-12

How can one remove or change the colour of the grey zero reference line that automatically appears in an R lattice barchart when origin = 0 is used?

Example:

require(lattice)

data <- data.frame(val = 1:2, id = c(-1, 1))

barchart(val ~ id, data, origin = 0)

I tried zero.line = F but couldn't get it to work.

Thank you!

CodePudding user response:

You need to add the parameter reference to the barchart after origin = 0. And set it to FALSE.

The code looks like this:

require(lattice)

data <- data.frame(val = 1:2, id = c(-1, 1))

barchart(val ~ id, data, origin = 0, reference = FALSE)

Output: enter image description here

To change the color of the reference line, you'll need to dig the parameters inside trellis.par.get(). Here you'll find myriad parameters related to your graph which will change the look of it as you wish.

In your case though, just to change the color of the reference line, the required parameter is reference.line$col.

Here's what the code looks like:

reference.line <- trellis.par.get("reference.line")
reference.line$col <- "red"
trellis.par.set("reference.line", reference.line)

Here's the output: enter image description here

The documentation for the trellis can be found here.

  • Related