geom_smooth和geom_line回归线创建

时间:2017-12-26 17:49:11

标签: r ggplot2

我在R中获得以下步骤的两个输出:

  1. ggplot(mario_kart, aes(y = totalPr, x = duration, color=cond)) + geom_point() + geom_smooth(method = "lm", se = 0)

  2. ggplot(mario_kart, aes(y = totalPr, x = duration, color=cond)) + geom_point() + geom_line(data=broom::augment(lm(totalPr~duration+cond,data=mario_kart)), aes(y=(.fitted)))

  3. 我想了解数据是如何被操纵的,以及我怎样才能知道我是否在ggplot中使用了正确的方法。

1 个答案:

答案 0 :(得分:0)

ggplot在R中生成图表。您应该使用数据框作为数据并操纵您感兴趣的变量。使用Aesthetics,您将描述您的数据与您的情节之间的关系。您还可以将图层指定为geom_pointgeom_histogram,geom_smooth等......

例如,

ggplot(mario_kart, aes(y = totalPr, x = duration, color=cond)) + geom_point() + geom_smooth(method = "lm", se = 0)

在这里,您使用了mario_kart数据,然后将变量totalPrduration作为您的美学。之后,您可以调用您使用的图层类型,即geom_point()geom_smooth。哪种图层有自己的参数,因此您定义了geom_smooth方法" lm"。

我建议您使用pipi运算符"%>%",这使您可以编写更易读的代码。

如下:

mario_kart %>% ggplot(aes(y = totalPr, x = duration, color=cond)) + geom_point() + geom_smooth(method = "lm", se = 0)
相关问题