Ggplot2:将STAT_Smooth回归线扩展到绘图区的整个x范围 [英] ggplot2: Extend stat_smooth regression line to entire x-range of plot area

查看:0
本文介绍了Ggplot2:将STAT_Smooth回归线扩展到绘图区的整个x范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的gggraph中有两条单独的回归线,每条线对应一个单独的变量。但是,与local对应的第二行不会延伸到整个图。是否有解决此问题的解决方法或使两条斜线在图形区域内均匀延伸的方法?

ggplot(metrics, aes(x=popDensity, y= TPB, color = factor(type))) + geom_point() +theme_minimal() + stat_smooth(method = "lm", se = FALSE) +
  geom_label_repel(aes(label= rownames(metrics)), size=3, show.legend = FALSE) +
  theme(axis.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=12)) +
  labs(x = expression(paste( "Populatin Density ", km^{2})), y = expression(paste("Rating")))+
  theme(legend.position="top", legend.direction="horizontal") + theme(legend.title=element_blank()) 

以下是数据示例:

> dput(metrics)
structure(list(popDensity = c(4308, 27812, 4447, 5334, 4662, 
2890, 1689, 481, 4100), TPB = c(2.65, 4.49, 2.37, 2.87, 3.87, 
2.95, 1.18, 1.62, 1.87), type = c("Global", "Global", "Global", 
"Global", "Global", "Global", "Local", "Local", "Local")), .Names = c("popDensity", 
"TPB", "type"), row.names = c("City1", "City2", "City3", "City4", 
"City5", "City6", "City7", "City8", "City9"), class = "data.frame")

推荐答案

添加fullrange = Tstat_smooth将使Fit覆盖整个绘图范围:

ggplot(metrics, aes(x = popDensity, y = TPB, color = factor(type))) +
    geom_point() +
    theme_minimal() +
    stat_smooth(method = "lm", se = FALSE, fullrange = T) +
    geom_label_repel(aes(label = rownames(metrics)),
                     size = 3,
                     show.legend = FALSE) +
    theme(axis.title = element_text(
        family = "Trebuchet MS",
        color = "#666666",
        face = "bold",
        size = 12
    )) +
    labs(x = expression(paste("Populatin Density ", km ^ {2})),
         y = expression(paste("Rating"))) +
    theme(legend.position = "top", legend.direction = "horizontal") +
    theme(legend.title = element_blank())

这篇关于Ggplot2:将STAT_Smooth回归线扩展到绘图区的整个x范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆