[R] colors in facet ggplot and geom_bar

efisio solazzo efisio.solazzo at jrc.ec.europa.eu
Thu Feb 25 10:46:12 CET 2016


Dear,
     Ok...I thought a hint would have been sufficient, but I'm still stuck.

based on the 'Data' dataframe:

Data
    mod_names err_type        value spec_comp sign.value          pos
1       mod1      var 2.970681e-03        ID          - 1.485341e-03
2       mod1     mMSE 1.881598e-01        ID            9.705057e-02
3       mod2      var 2.999713e-02        ID          - 1.499856e-02
4       mod2     mMSE 3.055899e-01        ID            1.827921e-01
5       mod3      var 1.570650e-01        ID          + 7.853252e-02
6       mod3     mMSE 2.430458e-01        ID            2.785880e-01
7       mod4      var 2.439812e-02        ID          + 1.219906e-02
8       mod4     mMSE 1.439277e-01        ID            9.636199e-02
9       mod1      var 6.476435e-01        DU          - 3.238218e-01
10      mod1     mMSE 1.575243e+00        DU            1.435265e+00
11      mod2      var 1.394583e-02        DU          - 6.972916e-03
12      mod2     mMSE 1.269346e+01        DU            6.360675e+00
13      mod3      var 1.338403e+00        DU          + 6.692017e-01
14      mod3     mMSE 8.775683e+00        DU            5.726245e+00
15      mod4      var 5.583281e-02        DU          - 2.791641e-02
16      mod4     mMSE 3.591430e+00        DU            1.851548e+00
17      mod1      var 2.810587e-01        SY          + 1.405294e-01
18      mod1     mMSE 8.157692e-01        SY            6.889433e-01
19      mod2      var 1.747732e-01        SY          + 8.738661e-02
20      mod2     mMSE 9.926385e-01        SY            6.710925e-01
21      mod3      var 1.113529e-01        SY          + 5.567645e-02
22      mod3     mMSE 6.747922e-01        SY            4.487490e-01
23      mod4      var 2.017077e+00        SY          + 1.008539e+00
24      mod4     mMSE 1.231198e+00        SY            2.632676e+00
25      mod1     bias 7.167534e+01        LT          + 3.583767e+01
26      mod1      var 3.287647e-06        LT          - 7.167534e+01
27      mod1     mMSE 2.113895e+00        LT            7.273229e+01
28      mod2     bias 5.979827e+01        LT          + 2.989914e+01
29      mod2      var 8.464560e-04        LT          - 5.979869e+01
30      mod2     mMSE 9.425981e-01        LT            6.027042e+01
31      mod3     bias 5.604628e+00        LT          + 2.802314e+00
32      mod3      var 4.698450e-02        LT          - 5.628121e+00
33      mod3     mMSE 6.434771e-01        LT            5.973351e+00
34      mod4     bias 1.614153e+02        LT          + 8.070763e+01
35      mod4      var 4.131887e+00        LT          + 1.634812e+02
36      mod4     mMSE 5.797821e+00        LT            1.684461e+02

I run the command:

ggplot(Data, aes(x=mod_names, y=value,
fill=err_type))+geom_bar(stat='identity', position='stack')  +
    facet_wrap(~spec_comp, nrow=1,
scales="free")                                +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5,
hjust=1.))        +
    theme(axis.title.x = element_blank())+ ylab(units) +
    geom_text ( aes(label=sign.value, y=pos))  +....

where err_type = c('bias', mMSe', 'var'). My questions are
1.  how to color-code only the mMSE portion of the bars based on a range of values 'r' from -1 to 1 (basically the correlation coefficient):

r
        ID  DU   SY   LT
mod1 -0.5 0.5  0.4 -0.1
mod2  0.6 0.2 -0.2  0.9
mod3  0.5 0.5  0.9 -1.0
mod4  0.4 0.3  0.2  0.0

2. and how to color-code the x-labels of the last panel (the LT one) based on the values of 'bias'.

Thanks a lot for your help.




On 22/02/2016 17:00, Jeff Newmiller wrote:
> By failing to provide a reproducible example and framing the desired 
> answer as 'any hint' you are effectively limiting yourself to 'any 
> hint' rather than a working example.
>
> The 'any hint' is that err_type should be a factor with levels in your 
> desired order, and you can then use any of the scale_fill_* functions 
> including scale_colour_manual to specify the colors in the same order 
> as the levels in the factor.
> -- 
> Sent from my phone. Please excuse my brevity.
>
> On February 22, 2016 7:24:17 AM PST, efisio solazzo 
> <efisio.solazzo at jrc.ec.europa.eu> wrote:
>
>     Dear,
>     I wonder if there is a way to 'play' with colors in facet ggplot and
>     geom_bar.
>
>     With reference to the attached figure, I'd like to
>     - color the green portion based on a numerical variable (say 1 to 10) on
>     all of the four panels and
>     - color-code the y labels based on the values of the 'bias' (red portion
>     of bars) only on the fourth panel.
>
>     the code to produce the plot is:
>
>     ggplot(Data, aes(x=mod_names, y=value,
>     fill=err_type))+geom_bar(stat='identity', position='stack')  +
>         facet_wrap(~spec_comp, nrow=1,
>     scales="free")                                +
>         theme(axis.text.x = element_text(angle = 90, vjust = 0.5,
>     hjust=1.))        +
>         theme(axis.title.x = element_blank())+ ylab(units) +
>         geom_text ( aes(label=sign.value, y=pos))  +....
>
>     where err_type = c('bias', mMSe', 'var')
>
>     thanks for any hint.
>


-- 
Efisio SOLAZZO, Ph.D.
European Commission, Joint Research Centre,
Institute for Environment and Sustainability,
TP123, Via E. Fermi, 2749 I-21027 Ispra (VA), Italy
Tel: +390332789944 Fax: +390332785837


	[[alternative HTML version deleted]]



More information about the R-help mailing list