You can use the geomabline function within facetgrid to draw multiple ablines per panel. Here's an example code snippet:
library(ggplot2)
# create data
df <- data.frame(x = 1:10, y = 1:10, group = c(rep("A", 5), rep("B", 5)))
# define slopes and intercepts for ablines
slopes <- c(0.5, 0.8)
intercepts <- c(1, 2)
# create plot with facet_grid and geom_point
ggplot(df, aes(x = x, y = y)) +
facet_grid(.~group) +
geom_point() +
# add ablines for each group
geom_abline(intercept = intercepts[1], slope = slopes[1]) +
geom_abline(intercept = intercepts[2], slope = slopes[2])
In this example, we first create a data frame with x and y values, as well as a grouping variable. We also define the slopes and intercepts of the ablines we want to draw.
Next, we create a plot with facetgrid and geompoint, as well as two instances of geom_abline within the same ggplot call. We specify the intercept and slope for each abline using the intercepts and slopes vectors we defined earlier. The resulting plot will have two panels, one for each group, with the corresponding ablines displayed in each panel.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-30 00:08:23 +0000
Seen: 8 times
Last updated: May 30 '23