R ggplot2,扫帚:如何在因子与连续变量的绘图中添加线条

R ggplot2,扫帚:如何在因子与连续变量的绘图中添加线条,r,ggplot2,dplyr,broom,R,Ggplot2,Dplyr,Broom,在下面的图中,我使用dplyr和broom显示了一组模型中的$R^2$值,这些模型适用于数据集的子集。我想用线连接这些点,或者像传统的点图一样,在每个点上画水平线。我该怎么做 代码 library(dplyr) library(ggplot2) library(gapminder) library(broom) # separate models for continents models <- gapminder %>% filter(continent != "Oce

在下面的图中,我使用
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()
,如下所示,但没有成功:

> gg + geom_hline(yintercept=levels(continent))
Error in levels(continent) : object 'continent' not found

这是可行的,但我会质疑连接线的使用。一般来说,这些线表示观察序列的逻辑进展,例如,随着时间的推移,事件的数量。这些数据中有这样的顺序吗

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
gg%
浏览(mod)%>%
突变(组=1)%>%
ggplot(aes(r平方,重新排序(大陆,r平方,组=组))+
几何路径()
几何点(尺寸=4)+
伊拉布(“大陆”)
游戏打得好

排序基于R^2值,通过使用
重新排序(大陆,R.squared)
实现。我没有考虑过这里的
geom_path()
,但这是按字母顺序连接点的,不是我想要的。啊!
geom_-line()
符合我的要求。
gg+geom_-line(aes(group=1))
工作正常!基本上,点在组内连接,通过将组设置为1,所有点都将连接。但是,不确定这些行是否提供了信息。使用
gg+geom_段(aes(xend=0,yend=…y…)获取水平线
。啊!比连接线更漂亮,信息量更大。我缺少的是
yend=…y..
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