【问题标题】:Unify boxplot factor group colours统一箱线图因子组颜色
【发布时间】:2014-05-24 18:08:00
【问题描述】:

我有点像 R 和 ggplot 新手,所以我正在努力将这些数据绘制为箱线图,通量在 y 上,周在 X 上,箱线图按物种分组(并且在每个物种分组处理中) )。

   Treatment   Species Week  Flux
1         L- Heisteria    1  0.19
2         L- Heisteria    1  0.03
3         L- Heisteria    1    NA
4         L- Heisteria    1  0.12
5         L- Simarouba    1  0.22
6         L- Simarouba    1  0.19
7         L- Simarouba    1    NA
8         L- Simarouba    1 -0.65
9          C Heisteria    1 -0.99
10         C Heisteria    1  0.10
11         C Heisteria    1  0.26
12         C Heisteria    1    NA
13         C Simarouba    1 -1.41
14         C Simarouba    1  0.17
15         C Simarouba    1    NA
16         C Simarouba    1  0.35
17        L+ Heisteria    1  0.71
18        L+ Heisteria    1  0.25
19        L+ Heisteria    1  0.08
20        L+ Heisteria    1  4.14
21        L+ Simarouba    1 -1.36
22        L+ Simarouba    1  0.06
23        L+ Simarouba    1 -0.65
24        L+ Simarouba    1 -0.25
25        L- Heisteria    2  0.31
26        L- Heisteria    2  0.15
27        L- Heisteria    2 -0.09
28        L- Heisteria    2 -0.08
29        L- Simarouba    2  0.04
30        L- Simarouba    2  0.06
31        L- Simarouba    2  0.05
32        L- Simarouba    2 -0.07
33         C Heisteria    2  0.20
34         C Heisteria    2  0.15
35         C Heisteria    2 -0.03
36         C Heisteria    2  0.18
37         C Simarouba    2  0.10
38         C Simarouba    2  0.08
39         C Simarouba    2  0.09
40         C Simarouba    2  0.05
41        L+ Heisteria    2  0.24
42        L+ Heisteria    2  0.09
43        L+ Heisteria    2  0.16
44        L+ Heisteria    2  0.11
45        L+ Simarouba    2    NA
46        L+ Simarouba    2  0.21
47        L+ Simarouba    2 -0.07
48        L+ Simarouba    2  1.51
49        L- Heisteria    3  0.15
50        L- Heisteria    3  0.07
51        L- Heisteria    3    NA
52        L- Heisteria    3 -1.02
53        L- Simarouba    3 -0.02
54        L- Simarouba    3  0.08
55        L- Simarouba    3 -0.06
56        L- Simarouba    3 -0.08
57         C Heisteria    3  0.23
58         C Heisteria    3  0.19
59         C Heisteria    3  0.09
60         C Heisteria    3 -0.10
61         C Simarouba    3  0.77
62         C Simarouba    3  0.07
63         C Simarouba    3  0.20
64         C Simarouba    3  0.62
65        L+ Heisteria    3  0.19
66        L+ Heisteria    3 -0.09
67        L+ Heisteria    3    NA
68        L+ Heisteria    3  0.06
69        L+ Simarouba    3    NA
70        L+ Simarouba    3 -0.17
71        L+ Simarouba    3  0.13
72        L+ Simarouba    3  0.64
73        L- Heisteria    4  0.13
74        L- Heisteria    4  0.54
75        L- Heisteria    4  0.18
76        L- Heisteria    4  3.59
77        L- Simarouba    4  0.00
78        L- Simarouba    4  0.10
79        L- Simarouba    4  0.20
80        L- Simarouba    4    NA
81         C Heisteria    4 -0.14
82         C Heisteria    4 -0.32
83         C Heisteria    4  0.21
84         C Heisteria    4  0.12
85         C Simarouba    4  0.10
86         C Simarouba    4    NA
87         C Simarouba    4  0.11
88         C Simarouba    4  0.42
89        L+ Heisteria    4  0.14
90        L+ Heisteria    4  0.05
91        L+ Heisteria    4  0.25
92        L+ Heisteria    4  0.74
93        L+ Simarouba    4    NA
94        L+ Simarouba    4  0.05
95        L+ Simarouba    4 -0.06
96        L+ Simarouba    4 -0.13

我可以使用此代码绘制数据

ggplot(treeflux, aes(Week, Flux, fill=interaction(Week, Species, Treatment), dodge=Species, Treatment)) + 
    stat_boxplot(geom ='errorbar') + 
    geom_boxplot()

它按照我想要的顺序给了我一个情节,但图例部分的颜色和项目太多了。我希望每个物种的治疗都是单一颜色的变体,并且传说读起来像这样“L- Heisteria”。

【问题讨论】:

    标签: r colors ggplot2 legend boxplot


    【解决方案1】:

    这个开始怎么样? (alpha 的图例需要稍作调整...)这比设置整个自定义填充颜色调色板并正确设置图例要容易得多...

    theme_set(theme_bw())  ## my aesthetic preference, also easier for    
                           ## distinguishing light vs dark colours
    ggplot(treeflux, aes(factor(Week), Flux, fill=Species, alpha=Treatment),
       dodge=Species, Treatment) + 
    stat_boxplot(geom ='errorbar') + 
    geom_boxplot()
    

    【讨论】:

    • 这比我尝试的要好得多,非常感谢!我假设最黑的盒子是 L+ 和最轻的 C?我遇到的另一个问题是尝试订购治疗,所以它是 L-、C 和 L+,但没有太多运气在这里浏览我理解的修复。
    • 是的。万一我没有得到第二次打击:(1)通过treeflux$Treatment <- factor(treeflux$Treatment,levels=c("L-","C","L+"))更改治疗顺序(2)在图例中使用override.aes可能会有所帮助(即在alpha中将填充颜色设置为黑色图例,以便您可以看到差异)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2017-06-19
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多