【目的】想用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