Different color/line type for vertical and horizontal ablines #65
-
How can we create different colors or line type for vertical and horizontal ablines? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
library(BoutrosLab.plotting.general);
set.seed(12345);
simple.data <- data.frame(
x = rnorm(800),
y = rnorm(800)
); Different color ablines are easy when both are horizontal/vertical. We can pass in a vector of line positions to either plot1 <- create.scatterplot(
formula = y ~ x,
data = simple.data,
abline.h = c(2, 1),
abline.lty = c(2, 3),
abline.lwd = 3,
abline.col = default.colours(2),
);
plot1; Changing color or line type for ablines when some are horizontal and some are vertical is less intuitive but still possible in BPG. plot2 <- create.scatterplot(
formula = y ~ x,
data = simple.data,
abline.h = c(2, NA),
abline.v = c(NA, 1),
abline.lty = c(2, 3),
abline.lwd = 3,
abline.col = default.colours(2),
);
plot2; lattice/latticeExtra solutionBefore I figured out the above solution, I came up with the following solution via lattice/latticeExtra which is quite useful when building complex plots. The idea is to manually create a layer with a vertical line via my.lty <- 3
vertical.abline <- latticeExtra::layer({
lattice::panel.abline(
v = 2,
lty = 3,
col = 'purple',
lwd = 4
)
}, data = list(lty = my.lty));
# Add plots together to combine layers
plot1 + vertical.abline; This latticeExtra layer approach works by appending panels to the existing plots. This means that we can augment any BPG plot with custom lattice code by adding layers together. Created on 2022-12-22 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
Different color ablines are easy when both are horizontal/vertical. We can pass in a vector of line positions to either
abline.h
orabline.v
and a vector of the same size (or length 1) toabline.lty
,abline.lwd
,abline.col
, etc.Changing color or line type for ablines when some are horizontal and some are vertical is less intuitive but still possible in BPG.
We still s…