是否可以为scatter.smooth添加标题?

时间:2015-12-02 12:00:42

标签: r scatter-plot

我经常使用scatter.smooth函数,但我想知道是否可以直接向此函数添加titlemain参数。我阅读了函数的描述,但没有找到可能性。我知道还有其他方法可以做到这一点,但我想尽可能保留这个。

d <- data.frame(x = sample(20, 500, prob=c(1:10, 10:1), replace = TRUE), 
                y = sample(20, 500, prob=c(1:10, 10:1), replace = TRUE),
                z = rnorm(500, 20, 4))
mo <- lm(y ~ z, d)
fig <- function(x) {
  scatter.smooth(fitted(x), residuals(x, type = "response"), col = "red")
  abline(0, 0, lty = 2)
  legend("topright", legend = c("loss", "0-0"), lty = c(1, 2))
}
fig(mo)

2 个答案:

答案 0 :(得分:2)

您查看scatter.smooth的帮助页面,您会看到...参数已传递给plot。因此,您可以向我们提出plot接受的任何论点。还main=

您还可以使用mtext为任何图表添加标题,这会在文字边距上添加文字。

所以,你可以这样做:

fig(mo)
mtext("My title", side=3, line=1)

或修改您的fig功能:

fig <- function(x, ...) {
  scatter.smooth(fitted(x), residuals(x, type = "response"), 
    col = "red", ...)
  abline(0, 0, lty = 2)
  legend("topright", legend = c("loss", "0-0"), lty = c(1, 2))
}
fig(mo, main="My title")

答案 1 :(得分:1)

只需将df['CustomWeights'] = df.groupby('CompanyName')['Date_Published'].transform(lambda x: dict_wt.get(len(x))) CompanyName Date_Published Stand_Alone Consolidated CustomWeights 0 A 31-03-2017 0.116712 0.044908 21.63 1 A 31-03-2016 0.228525 0.553351 18.54 2 A 31-03-2015 0.476527 0.913417 15.89 3 A 31-03-2014 0.989796 0.716775 13.62 4 A 31-03-2013 0.702358 0.880009 11.68 5 A 31-03-2012 0.531666 0.013267 10.01 6 A 31-03-2011 0.896103 0.351544 8.63 7 B 31-03-2017 0.405370 0.701944 11.00 8 B 31-03-2016 0.858221 0.450118 56.00 9 B 31-03-2015 0.163273 0.613447 21.00 10 B 31-03-2014 0.635888 0.570327 11.00 11 C 31-03-2017 0.680992 0.488191 55.55 12 C 31-03-2016 0.083883 0.682186 44.45 添加到main功能:

smooth

它有效