I'm trying to make multiple caterpillar plots for different dependent variables but same independent variables by ggplot.
With this trial data,
x1 <- c(1,2,3,4,5)
x2 <- c(4,5,6,1,2)
x3 <- c(7,8,3,2,1)
x4 <- c(3,4,5,6,7)
y1 <- c(10,9,11,18,13)
y2 <- c(12,20,21,19,11)
y3 <- c(18,16,12,11,18)
df <- base::data.frame(x1, x2, x3, x4, y1, y2, y3)
at first I tried to make the plots without facet .
lm1 <- lm(data = df,
y1 ~ x1 + x2 + x3 + x4)
lm_plot1 <- lm %>%
tidytext::tidy() %>%
ggplot2::ggplot(aes(x = term, y = estimate))
# Make two more lm_plots for y2, y3 in the same way...
# And put them on one screen by grid.arrange()
gridExtra::grid.arrange(lm_plot1, lm_plot2, lm_plot3)
Now I'd like to make faceted image. So I tried to make longer-data
df %>%
tidyr::pivot_longer(c(y1,y2,y3)) %>%
dplyr::rename(dv = name) %>%
group_by(dv)
but I don't know how to estimate each model after group_by function and to make faceted plots by ggplot.
