通过多种因素着色ggplot

时间:2014-06-18 04:52:28

标签: r ggplot2

我有以下data.frame:

mean    std.dev haplotype   tissue  genotype    replicate
-4.60517018598809   0   hap1    Brain - Amygdala    het 1
-4.60517018598809   0   hap1    Brain - Amygdala    het 2
-4.60517018598809   0   hap1    Brain - Amygdala    het 3
-4.60517018598809   0   hap1    Brain - Amygdala    hom 4
-4.60517018598809   0   hap1    Brain - Amygdala    hom 5
-2.4087395856822    2.0768401930033 hap1    Brain - Caudate (basal ganglia) het 6
-2.33089555210139   1.22454341404608    hap1    Brain - Caudate (basal ganglia) het 7
-1.99693736361828   0.942996853632486   hap1    Brain - Caudate (basal ganglia) het 8
-2.86324165970641   1.11947980609972    hap1    Brain - Caudate (basal ganglia) het 9
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) het 10
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) het 11
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) het 12
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) het 13
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) het 14
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) het 15
-1.90227490544572   1.3118533678251 hap1    Brain - Caudate (basal ganglia) het 16
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) hom 17
-2.28388358957011   1.66055627514132    hap1    Brain - Caudate (basal ganglia) hom 18
-3.18075788363206   1.31500305472203    hap1    Brain - Caudate (basal ganglia) hom 19
-4.60517018598809   0   hap1    Brain - Caudate (basal ganglia) hom 20
-2.99628156173586   1.45715378560505    hap1    Brain - Frontal Cortex (BA9)    het 21
-4.60517018598809   0   hap1    Brain - Frontal Cortex (BA9)    hom 22
-4.60517018598809   0   hap1    Brain - Frontal Cortex (BA9)    hom 23
-4.60517018598809   0   hap1    Brain - Frontal Cortex (BA9)    hom 24
-4.60517018598809   0   hap1    Brain - Frontal Cortex (BA9)    hom 25
-4.60517018598809   0   hap1    Brain - Frontal Cortex (BA9)    hom 26
-4.60517018598809   0   hap1    Brain - Hippocampus het 27
-3.23659805518369   1.30083340138879    hap1    Brain - Hippocampus het 28
-2.53609036554034   1.4874106990496 hap1    Brain - Hippocampus hom 29
-3.09405707251877   1.2828530102749 hap1    Brain - Hippocampus hom 30
-2.96062268602418   1.43770353838178    hap1    Brain - Hippocampus hom 31
-2.77211009494142   1.20159646882509    hap1    Brain - Hippocampus hom 32
-0.363072614934047  0.458485378139217   hap1    Brain - Hippocampus hom 33
-2.32474357410041   1.08129672683285    hap1    Brain - Hippocampus hom 34
-3.22834640282763   1.32315679407156    hap1    Brain - Hippocampus hom 35
-3.14051445752854   1.47382291072761    hap1    Brain - Hypothalamus    hom 36
-2.75712730132607   1.37273147849288    hap1    Brain - Hypothalamus    hom 37
-3.45020625780797   1.18533200381208    hap1    Brain - Hypothalamus    hom 38
-3.61103693406669   0.985810668100767   hap1    Brain - Hypothalamus    hom 39
-4.60517018598809   0   hap1    Brain - Hypothalamus    hom 40
-1.36046627633376   0.812257855129746   hap1    Brain - Hypothalamus    hom 41
-4.60517018598809   0   hap1    Brain - Hypothalamus    hom 42
-0.758614195344438  0.475371429738856   hap1    Brain - Hypothalamus    hom 43
-0.570751506996323  0.796705761206704   hap1    Brain - Hypothalamus    hom 44
-1.93529408140331   0.796764276230018   hap2    Brain - Amygdala    het 1
-1.2306095166114    1.12689741548906    hap2    Brain - Amygdala    het 2
-1.09158717166276   0.854886107611023   hap2    Brain - Amygdala    het 3
-2.41642059750677   1.69695118792728    hap2    Brain - Amygdala    hom 4
-0.975184099703771  0.516509361793386   hap2    Brain - Amygdala    hom 5
-4.60517018598809   0   hap2    Brain - Caudate (basal ganglia) het 6
-4.60517018598809   0   hap2    Brain - Caudate (basal ganglia) het 7
-4.60517018598809   0   hap2    Brain - Caudate (basal ganglia) het 8
-4.60517018598809   0   hap2    Brain - Caudate (basal ganglia) het 9
-1.72120397390227   1.07548128030902    hap2    Brain - Caudate (basal ganglia) het 10
-2.70202205468788   1.16342600462607    hap2    Brain - Caudate (basal ganglia) het 11
-3.18783283749423   1.05594737803958    hap2    Brain - Caudate (basal ganglia) het 12
-1.40699203946488   0.48542701269273    hap2    Brain - Caudate (basal ganglia) het 13
-1.87751485116197   0.935986612727037   hap2    Brain - Caudate (basal ganglia) het 14
-2.05710440082911   1.1599569476391 hap2    Brain - Caudate (basal ganglia) het 15
-4.60517018598809   0   hap2    Brain - Caudate (basal ganglia) het 16
-3.28172961347489   1.16473022131648    hap2    Brain - Caudate (basal ganglia) hom 17
-2.64643373422983   1.77404314601545    hap2    Brain - Caudate (basal ganglia) hom 18
-2.93809040163993   1.31659399315286    hap2    Brain - Caudate (basal ganglia) hom 19
-2.96028514861314   1.18901994150429    hap2    Brain - Caudate (basal ganglia) hom 20
-2.94427746735641   1.40561781396035    hap2    Brain - Frontal Cortex (BA9)    het 21
-3.41394405787045   1.17429314258876    hap2    Brain - Frontal Cortex (BA9)    hom 22
-1.82465028645704   0.610451251569348   hap2    Brain - Frontal Cortex (BA9)    hom 23
-2.32519547889492   0.974453244714196   hap2    Brain - Frontal Cortex (BA9)    hom 24
-0.0114334599008076 0.246630953756596   hap2    Brain - Frontal Cortex (BA9)    hom 25
-2.73835203211445   1.30531030511388    hap2    Brain - Frontal Cortex (BA9)    hom 26
-3.24035798829041   1.28781584752539    hap2    Brain - Hippocampus het 27
-3.12740315189855   1.29109745889288    hap2    Brain - Hippocampus het 28
-3.19807533474438   1.37389171459244    hap2    Brain - Hippocampus hom 29
-3.28828389936812   1.24441316535463    hap2    Brain - Hippocampus hom 30
-4.60517018598809   0   hap2    Brain - Hippocampus hom 31
-4.60517018598809   0   hap2    Brain - Hippocampus hom 32
-4.60517018598809   0   hap2    Brain - Hippocampus hom 33
-4.60517018598809   0   hap2    Brain - Hippocampus hom 34
-3.08249674935387   1.31417447050384    hap2    Brain - Hippocampus hom 35
-2.47020069079822   1.41087460604683    hap2    Brain - Hypothalamus    hom 36
-3.1010805356141    1.36869576597288    hap2    Brain - Hypothalamus    hom 37
-3.24746722228446   1.12654484449552    hap2    Brain - Hypothalamus    hom 38
-4.60517018598809   0   hap2    Brain - Hypothalamus    hom 39
-3.42959607400308   1.05586843920869    hap2    Brain - Hypothalamus    hom 40
-4.60517018598809   0   hap2    Brain - Hypothalamus    hom 41
-3.24002257386986   1.19536227038576    hap2    Brain - Hypothalamus    hom 42
-4.60517018598809   0   hap2    Brain - Hypothalamus    hom 43
-4.60517018598809   0   hap2    Brain - Hypothalamus    hom 44

我产生了这个数字: enter image description here

使用此代码:

replicate.plot = ggplot(data = replicate.plot.df, aes(factor(replicate), color = factor(haplotype)))
replicate.plot = replicate.plot + geom_boxplot(aes(fill = factor(haplotype),
lower = mean - std.dev, upper = mean + std.dev, middle = mean, ymin = mean - 3*std.dev, ymax = mean + 3*std.dev),
position = position_dodge(width = 0), width = 0.5, alpha = 0.5, stat="identity") + facet_wrap(~tissue, ncol = 4, scales = "free_x") +
scale_fill_manual(values = c("darkgreen","darkmagenta"),labels = c("haplotype.1","haplotype.2"),
name = "haplotype allele")+scale_colour_manual(values = c("darkgreen","darkmagenta"),
labels = c("haplotype.1","haplotype.2"),name = "haplotype allele")

因此,您可以看到我通过单倍型字段对框进行着色。我想要做的是有两组不同的颜色:一对是hap1和hap2,它们是基因型het,另一对是hap1和hap2,它们是基因型hom,并且具有这两个水平的传说。好。

1 个答案:

答案 0 :(得分:3)

为两个感兴趣的变量(单倍型和基因型)之间的相互作用创建一个新变量

replicate.plot.df$hapgeno<-interaction(replicate.plot.df$haplotype, 
    replicate.plot.df$genotype)

然后根据该变量着色

ggplot(data = replicate.plot.df, aes(factor(replicate))) +
  geom_boxplot(aes(fill = hapgeno, lower = mean - std.dev, upper = mean + std.dev, 
    middle = mean, ymin = mean - 3*std.dev, ymax = mean + 3*std.dev),
    position = position_dodge(width = .5), width = 0.5, alpha = 0.5, stat="identity") +    
  facet_wrap(~tissue, ncol = 3, scales = "free_x") +
  scale_fill_manual(values = c("darkgreen","darkmagenta","lightgreen","magenta"),
    labels = c("haplotype.1.het","haplotype.2.het","haplotype.1.hom","haplotype.2.hom"),
    name = "haplotype allele")

,这使得以下情节

enter image description here