【问题标题】:Plotting the Average Line of multiple lines绘制多条线的平均线
【发布时间】:2018-06-14 09:41:01
【问题描述】:

看,我确定以前有人问过这个问题,但我遇到了另一面墙,我想在一个绘图上绘制多条线的平均线。经过数小时的尝试,我似乎无法做到这一点。我知道这看起来很懒惰,我非常抱歉,但请尽我所能帮助兄弟。我只想在所有实验线上绘制平均线。我试过使用rowMeans,并尝试通过对我的数据进行分组来生成新的数据框,但没有成功。请。 https://www.dropbox.com/s/m1ao29xaudksanf/e1.txt?dl=0 https://www.dropbox.com/s/q0sf3hew2pco73s/c1.txt?dl=0

cc1L3 <- read.table('./Colony 1 Location 3/c1.txt', header=TRUE)
ec1L3 <- read.table('./Colony 1 Location 3/e1.txt', header=TRUE)
Col1Loc3 <- ggplot()+
geom_smooth(data=ec1L3, aes(Time,Current1, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current2, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current3, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current4, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current5, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current6, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current7, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current8, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current9, colour= 'experimental1L3'))+
geom_smooth(data=ec1L3, aes(Time,Current10, colour= 'experimental1L3'))+
###############
geom_smooth(data=cc1L3, aes(Time,Current1, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current2, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current3, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current4, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current5, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current6, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current7, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current8, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current9, colour='control1L3'))+
geom_smooth(data=cc1L3, aes(Time,Current10, colour='control1L3'))

【问题讨论】:

    标签: r ggplot2 sum average


    【解决方案1】:

    我不完全确定你在追求什么,但也许你想要这样的东西:

    cc1L3 <- read.table('https://www.dropbox.com/s/m1ao29xaudksanf/e1.txt?dl=1', header=TRUE)
    ec1L3 <- read.table('https://www.dropbox.com/s/q0sf3hew2pco73s/c1.txt?dl=1', header=TRUE)
    
    library(dplyr)
    
    df <- bind_rows(
      control = gather(cc1L3, variable, value, -Time),
      experimental = gather(ec1L3, variable, value, -Time),
      .id = 'treatment'
    )
    
    ggplot(df, aes(Time, value, color = treatment)) +
      geom_smooth(aes(group = interaction(variable, treatment)), se = FALSE, size = 0.5) +
      geom_smooth(se = FALSE, size = 2)
    

    重塑数据至关重要。

    【讨论】:

    • 我认为您是反向阅读文件。您将名为“c1.txt”的文件分配给ec1L3,反之亦然
    • 抱歉,我做到了。对实际解决方案有影响吗?你确定可以从这里拿走吗?
    • 我不是 OP,但我认为他们可以切换回来
    【解决方案2】:

    单独计算mean()呢?

    ec1L3$mean <- apply(ec1L3[2:ncol(ec1L3)],1, mean) # Calculating by row the mean
    cc1L3$mean <- apply(cc1L3[2:ncol(cc1L3)],1, mean) # Calculating by row the mean
    ## Add each line to each graphic 
    geom_smooth(data=ec1L3, aes(Time,mean, colour= 'experimental1L3'))
    geom_smooth(data=cc1L3, aes(Time,mean, colour= 'control1L3'))
    

    我希望它有所帮助。干杯!

    【讨论】:

    • 注意有一个rowMeans函数!另外,您对我的应用调用错误,2:ncol(ec1L3) 只是从 2 到 11 的向量....
    • 是的,因为他数据集的第一列是x线
    • 但你不是apply对数据的功能,你是申请2:11。也许你试图做例如apply(ec1L3[2:ncol(ec1L3)],1, mean)
    • 你是对的。我没有看到我的那个错误。我已经更正了:)
    猜你喜欢
    • 2019-02-27
    • 1970-01-01
    • 2022-01-22
    • 2014-02-17
    • 2022-01-22
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多