在下图中,我使用dplyr
和broom
从一组适合数据集子集的模型中显示$ R ^ 2 $的值。我想逐行连接点,或者在每个点上绘制水平线,就像在传统的点图中一样。我怎么能这样做?
代码
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
我尝试添加geom_line()
,并且无法理解我在这种情况下如何使用group
美学
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))
或者,我尝试geom_line()
,如下所示,但没有成功:
> gg + geom_hline(yintercept=levels(continent))
Error in levels(continent) : object 'continent' not found
答案 0 :(得分:1)
这有效,但我会质疑使用连接线。通常,这样的线表明观察序列的逻辑进展,例如,一段时间内发生的事件。这些数据中是否有这样的订单?
gg <-
models %>%
glance(mod) %>%
mutate(group = 1) %>%
ggplot(aes(r.squared, reorder(continent, r.squared), group = group) ) +
geom_path() +
geom_point(size=4) +
ylab("Continent")
gg