简单的R中的纵向数据图

时间:2018-04-08 20:53:29

标签: r ggplot2 longitudinal

我有一个简单的问题,但我花了好几天试图解决这个问题。我有一个带有重复措施的长数据文件,如下所示。

def power(theNumber, thePower):
#basically, multiply the number for power times

try:
    theNumber=int(theNumber)
    thePower=int(thePower)
    if theNumber == 0:
        return 0
    elif thePower == 0:
        return 1
    else:
        return theNumber * power(theNumber,thePower-1)

except exception as err:
    return 'Only digits are allowed as input'   

我想描绘一下性别抑郁症的趋势。然而,我的所有尝试都导致每个ID都有自己的行。

我只想要一条线用于MALE,一条线用于FEMALE。

1 个答案:

答案 0 :(得分:1)

这是一个ggplot2解决方案。在这里我假设当你说你想要"趋势"时,你想要为数据拟合一些模型。在这里,我为每个性别拟合一个线性模型。

我不认为这张图很好,因为它没有迹象表明相同ID的点是如何连接的。您可以通过多种方式处理此问题,如果您只有几个主题,则可以将shape映射到ID,或者将它们与geom_path连接并将group映射到{{1} }}

ID

enter image description here

对于平均线,您必须将每组的均值预先计算为新的数据帧。在这里,我使用了library(ggplot2) df <- read.table( text = " ID DEPRESSION TIME GENDER 1 5 1 MALE 1 5 2 MALE 1 4 3 MALE 2 3 1 MALE 2 6 2 MALE 2 8 3 MALE 3 2 1 FEMALE 3 2 2 FEMALE 3 2 3 FEMALE ", header = TRUE ) ggplot(df, aes(x = TIME, y = DEPRESSION, color = GENDER)) + geom_point() + stat_smooth(method = "lm", se = FALSE) &#39; dplyrgroup_by,给了我summarise。然后我只能通过修改df_summarised参数来为geom_hline图层使用新数据框。

data

enter image description here