ggplot创建关联图

时间:2012-09-14 18:12:09

标签: r ggplot2

我正在尝试使用ggplot2创建变量之间的线性相关图:

dput(sum)
structure(list(Date = structure(c(15218, 15248, 15279, 15309, 
15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553), class = "Date"), 
    Teams = c(87, 142, 173, 85, 76, 76, 93, 49, 169, 139, 60, 
    120), Scores = c("67101651", "62214988", "63183320", "66750198", 
    "61483322", "67546775", "75290893", "60713372", "77879142", 
    "70290302", "83201853", "83837301")), .Names = c("Date", 
"Teams", "Scores"), row.names = c(NA, 12L), class = "data.frame")

这是我的命令:

ggplot(sum, aes(x = Scores, y = Teams, group=1)) + 
    geom_smooth(method = "lm", se=TRUE, size=2, formula = lm(Teams ~ Scores))

我收到此错误:

Error in eval(expr, envir, enclos) : object 'Teams' not found

任何想法?

2 个答案:

答案 0 :(得分:1)

如果要为例如线性模型指定公式,请使用y ~ poly(x, 1)。只要您想要一个简单的线性回归(它是formula的默认值),您就不需要更改method = "lm"参数:

ggplot(sum, aes(x = Scores, y = Teams, group = 1)) +
  geom_smooth(method = "lm", formula = y ~ poly(x, 1), se = TRUE, size = 2)

如果您不希望此变量属于分类,我还建议您使用Scores作为数值(as.numeric(Scores))。这会改变回归线。

Score作为分类变量:

categorial

Score作为数字变量:

numeric

答案 1 :(得分:1)

这是使用 stat_cor 包中的 ggpubr 的另一个选项。此代码将绘制您的点并显示相关性和 p 值。如果您有非正态数据,可以将“pearson”更改为“spearman”。

ggplot(sum, aes(x = Scores, y = Teams, group = 1)) +
  geom_point(aes()) +
  geom_smooth(method = "lm", se = TRUE, size = 2) +
  stat_cor(method = "pearson", cor.coef.name = "r", vjust = 1, size = 4)