I would like to plot the output from more than one regression.
MWE:
df <- data.frame(
y1 = runif(100),
y2 = runif(100),
x = c(rep("A",times=50), rep("B",times=50))
)
If I want to plot the intercept and coefficients, I can use dwplot():
m0 <- lm(y1 ~ factor(x), data = df)
m1 <- lm(y2 ~ factor(x), data = df)
dwplot(list(m0, m1), show_intercept=T) + theme_bw()
However, I am interested in the ls means (computed with the package library(lsmeans)
). For this case, dwplot()
gives me an error.
lsm0 <- lsmeans(m0,~ factor(x), data = df)
lsm1 <- lsmeans(m1,~ factor(x), data = df)
dwplot(list(lsm0, lsm1), show_intercept=T) + theme_bw()
What are possible solutions?
You could use the results from both object and create one dataframe to make your own ggplot with geom_pointrange
like this:
library(dotwhisker)
library(lsmeans)
m0 <- lm(y1 ~ factor(x), data = df)
m1 <- lm(y2 ~ factor(x), data = df)
lsm0 <- lsmeans(m0, ~ factor(x), data = df)
lsm1 <- lsmeans(m1, ~ factor(x), data = df)
library(tidyverse)
as.data.frame(lsm0) %>%
mutate(model = "model 1") %>%
bind_rows(., as.data.frame(lsm1) %>%
mutate(model = "model 2")) %>%
ggplot(aes(x = lsmean, y = x, color = model, xmin = lower.CL, xmax = upper.CL)) +
geom_pointrange(position = position_dodge(width = 0.2)) +
theme_bw()
Created on 2023-04-07 with reprex v2.0.2