简体   繁体   English

在ggplot中显示多条趋势线

[英]Display multiple trend lines in ggplot

I have a table as follows,我有一张表如下,

    id    | membership |   month    |   year     |   numberofXPurchased
----------+------------+------------+------------+-------------------
     1    |    05      |    02      |   2014     |          5
     1    |    06      |    03      |   2014     |          7     
     1    |    07      |    04      |   2014     |          3
     2    |    01      |    11      |   2014     |          2
     2    |    02      |    12      |   2014     |          1
     2    |    03      |    01      |   2015     |          4

I created a line graph using ggplot to identify the correlation between the membership period and the number of time X was purchased我使用 ggplot 创建了一个折线图来确定会员期和购买 X 的次数之间的相关性

ggplot(data = df, aes (x = memberMonths, y=numberofXPurchased, group=id, color = id)) +
geom_line() +
geom_point() + 
theme(legend.position = "none") +
labs(y="Membership in Months", x = "X purchased")

This produces a line graph as expected, but as I have over 100000 rows of data, the plot is not interpretable.这会按预期生成折线图,但由于我有超过 100000 行数据,因此该图无法解释。 So I'm trying to display only trend lines instead of the lines representing each id, where 1 trend line to represent the entire plot, and a set of trend lines for each 'year' (Maybe in another plot).所以我试图只显示趋势线而不是代表每个 id 的线,其中 1 条趋势线代表整个图,以及每个“年份”的一组趋势线(可能在另一个图中)。

Adding添加

stat_smooth( method="lm") or
geom_smooth(method = "lm")

Only adds the trend line to the existing plot, but I want the trendline instead of the data from df只将趋势线添加到现有图中,但我想要趋势线而不是来自 df 的数据

Is there an efficient way to do this, Thanks in advance有没有一种有效的方法来做到这一点,提前致谢

You can use geom_smooth(), with the 'lm' option gives a linear model你可以使用 geom_smooth(),'lm' 选项给出了一个线性模型

geom_smooth(method = "lm")

Show your code would look like..显示您的代码看起来像..

    ggplot(data = df, aes (x = memberMonths, y=numberofXPurchased,group=id, color = id)) +
    geom_smooth(method = "lm") +
    geom_point() + 
    theme(legend.position = "none") +
    labs(y="Membership in Months", x = "X purchased")

As it appears geom_smooth() needs geom_point() to give the correct trendline I would use alpha=0 within the geom_point() call.看起来geom_smooth()需要geom_point()来给出正确的趋势线,我会在geom_point()调用中使用alpha=0

    ggplot(data = df, aes (x = memberMonths, y=numberofXPurchased,group=id, color = id)) +
    geom_smooth(method = "lm") +
    geom_point(alpha=0) + 
    theme(legend.position = "none") +
    labs(y="Membership in Months", x = "X purchased")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM