如何将多个图组合成一个

时间:2018-06-04 15:32:11

标签: r ggplot2

我试图绘制平均值,标准偏差和我的数据平均值的标准误差

数据表看起来像

freq mean-minP mean-correctedminP sd-minP sd-correctedminP se-minP se-correctedminP
0.04 0.002253326 0.2570858 0.00281535 0.2299418 5.365941e-06 0.0004382595  
0.06 0.002215208 0.2555567 0.002724357 0.2261158 4.193085e-06 0.0003480172    
0.08 0.002229885 0.2580928 0.002688148 0.2257434 4.788916e-06 0.0004021602    
0.10 0.002259921 0.2614918 0.002692339 0.2255382 5.17569e-06 0.0004335695    
0.12 0.002252171 0.2615271 0.00266768 0.2239421 5.341592e-06 0.0004484073    
0.14 0.002279967 0.2620723 0.002773566 0.226358 5.726879e-06 0.0004673856    
0.16 0.00227563 0.2634223 0.002696895 0.2248959 5.812342e-06 0.000484695    
0.18 0.002325724 0.2680895 0.002727271 0.226968 6.08316e-06 0.0005062506    
0.20 0.002338423 0.2698884 0.002716278 0.2267581 6.19585e-06 0.0005172369

代码是

library(ggplot2)

x = read.table("mean-sd-se.txt", header =T)

as.data.frame.matrix(x) 

class(x)

jpeg('mean-minP.jpg')

ggplot(x, aes(freq, mean.minP)) +
  geom_point() +
  geom_errorbar(aes(ymin = mean.minP - se.minP, ymax = mean.minP + se.minP))

dev.off()

jpeg('mean-correctedminP.jpg')

ggplot(x, aes(freq, mean.correctedminP )) +
  geom_point() +
  geom_errorbar(aes(ymin = mean.correctedminP - se.correctedminP, ymax = mean.correctedminP + se.correctedminP))

dev.off()

第三个图表是

freq ben-minP sd se
0.04 2.272638e-05 2.446807e-05 2.446807e-06
0.06 2.913836e-05 2.863156e-05 2.863156e-06
0.08 3.554738e-05 3.428234e-05 3.428234e-06
0.10 2.748583e-05 2.544388e-05 2.544388e-06
0.12 2.699006e-05 2.334158e-05 2.334158e-06
0.14 2.688554e-05 2.607751e-05 2.607751e-06
0.16 2.791795e-05 2.749683e-05 2.749683e-06
0.18 3.163485e-05 3.127313e-05 3.127313e-06
0.20 2.905028e-05 3.089178e-05 3.089178e-06

我想把所有这些情节都放到一个不同颜色的地方

1 个答案:

答案 0 :(得分:1)

在那里,最棘手的部分是塑造数据,我使用tidyverse

library(tidyverse)
tidy_df <- df %>%
  select(freq,mean.minP,mean.correctedminP,se.minP,se.correctedminP) %>%
  gather(,,-1) %>%
  separate(key,into = c("fun","var")) %>%
  spread(fun,value)

tidy_df
#    freq           var        mean           se
# 1  0.04 correctedminP 0.257085800 4.382595e-04
# 2  0.04          minP 0.002253326 5.365941e-06
# 3  0.06 correctedminP 0.255556700 3.480172e-04
# 4  0.06          minP 0.002215208 4.193085e-06
# 5  0.08 correctedminP 0.258092800 4.021602e-04
# 6  0.08          minP 0.002229885 4.788916e-06
# 7  0.10 correctedminP 0.261491800 4.335695e-04
# 8  0.10          minP 0.002259921 5.175690e-06
# 9  0.12 correctedminP 0.261527100 4.484073e-04
# 10 0.12          minP 0.002252171 5.341592e-06
# 11 0.14 correctedminP 0.262072300 4.673856e-04
# 12 0.14          minP 0.002279967 5.726879e-06
# 13 0.16 correctedminP 0.263422300 4.846950e-04
# 14 0.16          minP 0.002275630 5.812342e-06
# 15 0.18 correctedminP 0.268089500 5.062506e-04
# 16 0.18          minP 0.002325724 6.083160e-06
# 17 0.20 correctedminP 0.269888400 5.172369e-04
# 18 0.20          minP 0.002338423 6.195850e-06

ggplot(tidy_df, aes(freq, mean,color=factor(var))) +
  geom_point() +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se))

抱歉,我无法从此计算机上传图片。那是你想要的吗?值非常不同,因此不太可读。

如果你想在一张图上放置2个刻度,那么ggplot2就不那么简单了(我们可以重新缩放数据)。

数据

df <- read.table(text=
"freq mean-minP mean-correctedminP sd-minP sd-correctedminP se-minP se-correctedminP
0.04 0.002253326 0.2570858 0.00281535 0.2299418 5.365941e-06 0.0004382595  
0.06 0.002215208 0.2555567 0.002724357 0.2261158 4.193085e-06 0.0003480172    
0.08 0.002229885 0.2580928 0.002688148 0.2257434 4.788916e-06 0.0004021602    
0.10 0.002259921 0.2614918 0.002692339 0.2255382 5.17569e-06 0.0004335695    
0.12 0.002252171 0.2615271 0.00266768 0.2239421 5.341592e-06 0.0004484073    
0.14 0.002279967 0.2620723 0.002773566 0.226358 5.726879e-06 0.0004673856    
0.16 0.00227563 0.2634223 0.002696895 0.2248959 5.812342e-06 0.000484695    
0.18 0.002325724 0.2680895 0.002727271 0.226968 6.08316e-06 0.0005062506    
0.20 0.002338423 0.2698884 0.002716278 0.2267581 6.19585e-06 0.0005172369",
h=T)