In the plot below, I show values of $R^2$ from a set of models fit to subsets of a data set using dplyr
and broom
. I'd like to connect the points by lines, or else draw horizontal lines to each point, as in a traditional dot plot. How can I do this?
Code
library(dplyr)
library(ggplot2)
library(gapminder)
library(broom)
# separate models for continents
models <- gapminder %>%
filter(continent != "Oceania") %>%
group_by(continent) %>%
do(mod = lm(lifeExp ~ year + pop + log(gdpPercap),
data=.)
)
models %>% glance(mod)
gg <-
models %>%
glance(mod) %>%
ggplot(aes(r.squared, reorder(continent, r.squared))) +
geom_point(size=4) +
ylab("Continent")
gg
I tried adding geom_line()
, and can't understand how I use a group
aesthetic in this context
gg + geom_line()
geom_path: Each group consists of only one observation. Do you need to adjust
the group aesthetic?
gg + geom_line(aes(group=continent))
Alternatively, I tried geom_line()
, as below, without success:
> gg + geom_hline(yintercept=levels(continent))
Error in levels(continent) : object 'continent' not found