美文网首页
画多个系列折线图,并添加平均值折线

画多个系列折线图,并添加平均值折线

作者: 阳阳_e5f8 | 来源:发表于2018-05-08 20:34 被阅读0次

    【目的】想用R画多个系列数据的折线图,并有一条代表性的折线(如平均值折线)。

    【效果】


    image

    参考:Plotting individual observations and group means with ggplot2 | R-bloggers

    
    #### Test of plot line group ####
    install.packages("devtools")
    devtools::install_github("drsimonj/ourworldindata")
    
    # Individual-observations data
    library(ourworldindata)
    library(tidyr)
    library(dplyr)
    library(ggplot2)
    head(financing_healthcare)
    id <- financing_healthcare %>% 
      filter(continent %in% c("Oceania", "Europe") & between(year, 2001, 2005)) %>% 
      select(continent, country, year, health_exp_total) %>% 
      na.omit() # 筛选2001-2005年两个continent的health_exp_total数据
    id
    # 画多个系列折线图
    ggplot(id, aes(x = year, y = health_exp_total, group = country,color = continent)) +
      geom_line()
    
    gd <- id %>% 
      group_by(continent,year) %>% 
      summarise(health_exp_total = mean(health_exp_total))  # 按continent统计平均值
    gd
    
    # 加入平均值折线,注意与上面的不同
    ggplot(id, aes(x = year, y = health_exp_total,  color = continent)) +
      geom_line(aes(group = country)) + # 所有系列折线,注意要在此加入group信息
      geom_line(data = gd) # 平均值折线
    
    # 设置平均值折线的粗细和透明度,使其凸现出来
    ggplot(id, aes(x = year, y = health_exp_total, color = continent)) +
      geom_line(aes(group = country), alpha = .3) +  # 所有折线的透明度
      geom_line(data = gd, alpha = .8, size = 3) +   # 平均值折线的透明度和粗细
      theme_bw() +
      labs(
        title = "Changes in healthcare spending\nacross countries and world regions",
        x = NULL,
        y = "Total healthcare investment ($)",
        color = NULL
      )
    
    ##### 用自己数据作图 #####
    > h12.m
    # A tibble: 179,676 x 7
       Gene.ID   Cat   group     group2    material time  log2_normalized_counts
       <fct>     <chr> <fct>     <fct>     <chr>    <chr>                  <dbl>
     1 AT1G01010 Cat.7 NA        NA        hid1     12on                    2.93
     2 AT1G01020 Cat.7 NA        NA        hid1     12on                    3.17
     3 AT1G01030 Cat.5 phyB_only phyB_down hid1     12on                    1.11
     4 AT1G01040 Cat.7 NA        NA        hid1     12on                    6.24
     5 AT1G01046 Cat.7 NA        NA        hid1     12on                   -2.89
     6 AT1G01050 Cat.7 NA        NA        hid1     12on                    4.94
     7 AT1G01060 Cat.7 NA        NA        hid1     12on                    4.54
     8 AT1G01070 Cat.9 NA        NA        hid1     12on                   NA   
     9 AT1G01080 Cat.7 NA        NA        hid1     12on                    4.58
    10 AT1G01090 Cat.9 NA        NA        hid1     12on                    5.86
    # ... with 179,666 more rows
    
    # 筛选在12ON时间点co_up且隶属于Cat1基因,并计算平均值
    h12.up1 <- h12.m %>% 
      filter(Cat == "Cat.1" & group2 == "co_up" & material =="WT") 
    h12.coup1 <- h12.m %>% 
      filter(Cat == "Cat.1" & group2 == "co_up") %>% 
      group_by(Cat,material,time) %>% 
      summarise(log2_normalized_counts=mean(log2_normalized_counts, na.rm = T))
    
    ##### 数据如下所示:#####
    > h12.up1
    # A tibble: 27 x 7
       Gene.ID   Cat   group  group2 material time  log2_normalized_counts
       <fct>     <chr> <fct>  <fct>  <chr>    <chr>                  <dbl>
     1 AT1G28670 Cat.1 common co_up  WT       12on                  0.566 
     2 AT2G28200 Cat.1 common co_up  WT       12on                 -1.94  
     3 AT3G06435 Cat.1 common co_up  WT       12on                  0.0694
     4 AT3G13404 Cat.1 common co_up  WT       12on                 -0.344 
     5 AT3G23430 Cat.1 common co_up  WT       12on                  1.14  
     6 AT3G28180 Cat.1 common co_up  WT       12on                  1.80  
     7 AT3G52470 Cat.1 common co_up  WT       12on                  1.47  
     8 AT5G14930 Cat.1 common co_up  WT       12on                 -0.995 
     9 AT5G46710 Cat.1 common co_up  WT       12on                 -2.26  
    10 AT1G28670 Cat.1 common co_up  WT       24on                  4.94  
    # ... with 17 more rows
    > h12.coup1
    # A tibble: 9 x 4
    # Groups:   Cat, material [?]
      Cat   material time  log2_normalized_counts
      <chr> <chr>    <chr>                  <dbl>
    1 Cat.1 hid1     12on                  1.49  
    2 Cat.1 hid1     24on                  3.56  
    3 Cat.1 hid1     48on                  3.57  
    4 Cat.1 phyB     12on                  2.21  
    5 Cat.1 phyB     24on                  0.466 
    6 Cat.1 phyB     48on                 -1.54  
    7 Cat.1 WT       12on                 -0.0547
    8 Cat.1 WT       24on                  3.18  
    9 Cat.1 WT       48on                  4.64
    
    ##### 作图并保存图像 #####
    ggplot(h12.up1, aes(x=time, y=log2_normalized_counts, color=material))+
      geom_line(aes(group=Gene.ID), alpha=.4,linetype= 2)+
      geom_line(data = h12.coup1, aes(group=material), alpha=1,size=1.5)+ #要加入group=material
      scale_x_discrete(labels=c("12hr","24hr","48hr")) + # change x-grid labels
      xlab("Time") + ylab(expression(paste("log"[2],"(CPM)"))) + # change x-axis labels
      theme_bw()
    ggsave("Cat1_12hr_co_up.png", width =10, height = 8,units = "cm", dpi=300)
      
    
    • 输出图像如下:


      image.png

    相关文章

      网友评论

          本文标题:画多个系列折线图,并添加平均值折线

          本文链接:https://www.haomeiwen.com/subject/ixnwrftx.html