unique scale color ggplot2

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

unique scale color ggplot2

catalin roibu
Dear R users,

I want to create a plot for multiple sites and to keep the same color range
scale (the correlation values range from -0.5 to 0.7 for all data, but I
have sites with different min and max).

I used this code:
cols<-c("#0288D1", "#039BE5", "#03A9F4","#29B6F6", "#4FC3F7", "#FFCDD2",
"#E57373", "#F44336", "#E53935", "#D32F2F", "#C62828", "#B71C1C")
zCuts <-seq(-.5, 0.6, by = 0.1)
p<-ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill = cut(cor,
zCuts))) +
  geom_tile() +
  scale_fill_manual(values=cols)

but for each site the scale color is different.


Please help me to solve this problem!

Thank you!


Bests!

Catalin




--

-
-
Catalin-Constantin ROIBU
Lecturer PhD, Forestry engineer
Forestry Faculty of Suceava
Str. Universitatii no. 13, Suceava, 720229, Romania
office phone      +4 0230 52 29 78, ext. 531
mobile phone    +4 0745 53 18 01
FAX:                +4 0230 52 16 64
silvic.usv.ro <http://www.usv.ro/>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: unique scale color ggplot2

R help mailing list-2
Dear Catalin,

use scale_fill_gradient() and set fixed limits

ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill = cut(cor,
zCuts))) +
  geom_tile() +
  scale_fill_gradient(limits = c(-0.7, 0.7))

Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
[hidden email]
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>


Op di 7 jul. 2020 om 12:02 schreef Catalin Roibu <[hidden email]>:

> Dear R users,
>
> I want to create a plot for multiple sites and to keep the same color range
> scale (the correlation values range from -0.5 to 0.7 for all data, but I
> have sites with different min and max).
>
> I used this code:
> cols<-c("#0288D1", "#039BE5", "#03A9F4","#29B6F6", "#4FC3F7", "#FFCDD2",
> "#E57373", "#F44336", "#E53935", "#D32F2F", "#C62828", "#B71C1C")
> zCuts <-seq(-.5, 0.6, by = 0.1)
> p<-ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill = cut(cor,
> zCuts))) +
>   geom_tile() +
>   scale_fill_manual(values=cols)
>
> but for each site the scale color is different.
>
>
> Please help me to solve this problem!
>
> Thank you!
>
>
> Bests!
>
> Catalin
>
>
>
>
> --
>
> -
> -
> Catalin-Constantin ROIBU
> Lecturer PhD, Forestry engineer
> Forestry Faculty of Suceava
> Str. Universitatii no. 13, Suceava, 720229, Romania
> office phone      +4 0230 52 29 78, ext. 531
> mobile phone    +4 0745 53 18 01
> FAX:                +4 0230 52 16 64
> silvic.usv.ro <http://www.usv.ro/>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: unique scale color ggplot2

Rasmus Liland-3
On 2020-07-07 12:44 +0200, Thierry Onkelinx via R-help wrote:

> Op di 7 jul. 2020 om 12:02 schreef Catalin Roibu <[hidden email]>:
> >
> > Dear R users,
> >
> > I want to create a plot for multiple
> > sites and to keep the same color
> > range scale (the correlation values
> > range from -0.5 to 0.7 for all data,
> > but I have sites with different min
> > and max).
> >
> > I used this code:
> > cols<-c("#0288D1", "#039BE5", "#03A9F4","#29B6F6", "#4FC3F7", "#FFCDD2",
> > "#E57373", "#F44336", "#E53935", "#D32F2F", "#C62828", "#B71C1C")
> > zCuts <-seq(-.5, 0.6, by = 0.1)
> > p<-ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill = cut(cor,
> > zCuts))) +
> >   geom_tile() +
> >   scale_fill_manual(values=cols)
> >
> > but for each site the scale color is different.
> >
>
> Dear Catalin,
>
> use scale_fill_gradient() and set fixed limits
>
> ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill = cut(cor,
> zCuts))) +
>   geom_tile() +
>   scale_fill_gradient(limits = c(-0.7, 0.7))

Hmm ... what might df1 have looked like
... creating a df with columns spei,
month, cor, and zCuts containing 1:5
doesn't do the trick ...

        df1 <-
          data.frame(
            spei=1:5,
            month=1:5,
            cor=1:5,
            zCuts=1:5
          )
        cols <-
          c("#0288D1", "#039BE5",
            "#03A9F4","#29B6F6", "#4FC3F7",
            "#FFCDD2", "#E57373", "#F44336",
            "#E53935", "#D32F2F", "#C62828",
            "#B71C1C")
        zCuts <- seq(-.5, 0.6, by=0.1)
        filename <- "/tmp/catalin1.png"
        width <- 800
        height <- 600
        res <- 150
        png(filename=filename, width=width, height=height, res=res)
        mapping <- ggplot2::aes(
          x = as.factor(spei),
          y = as.factor(month),
          fill = cut(cor, zCuts)
        )
        p <- ggplot2::ggplot(df1, mapping=mapping) +
           ggplot2::geom_tile() +
           ggplot2::scale_fill_gradient(limits = c(-0.7, 0.7))
        #   ggplot2::scale_fill_manual(values=cols)
        p
        dev.off()

... which only produces the error

        Error: Discrete value supplied to continuous scale
        Execution halted

V

r

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: unique scale color ggplot2

R help mailing list-2
Don't use the cut() function.

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
[hidden email]
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>


Op di 7 jul. 2020 om 18:42 schreef Rasmus Liland <[hidden email]>:

> On 2020-07-07 12:44 +0200, Thierry Onkelinx via R-help wrote:
> > Op di 7 jul. 2020 om 12:02 schreef Catalin Roibu <[hidden email]
> >:
> > >
> > > Dear R users,
> > >
> > > I want to create a plot for multiple
> > > sites and to keep the same color
> > > range scale (the correlation values
> > > range from -0.5 to 0.7 for all data,
> > > but I have sites with different min
> > > and max).
> > >
> > > I used this code:
> > > cols<-c("#0288D1", "#039BE5", "#03A9F4","#29B6F6", "#4FC3F7",
> "#FFCDD2",
> > > "#E57373", "#F44336", "#E53935", "#D32F2F", "#C62828", "#B71C1C")
> > > zCuts <-seq(-.5, 0.6, by = 0.1)
> > > p<-ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill =
> cut(cor,
> > > zCuts))) +
> > >   geom_tile() +
> > >   scale_fill_manual(values=cols)
> > >
> > > but for each site the scale color is different.
> > >
> >
> > Dear Catalin,
> >
> > use scale_fill_gradient() and set fixed limits
> >
> > ggplot(df1, aes(x=as.factor(spei), y=as.factor(month), fill = cut(cor,
> > zCuts))) +
> >   geom_tile() +
> >   scale_fill_gradient(limits = c(-0.7, 0.7))
>
> Hmm ... what might df1 have looked like
> ... creating a df with columns spei,
> month, cor, and zCuts containing 1:5
> doesn't do the trick ...
>
>         df1 <-
>           data.frame(
>             spei=1:5,
>             month=1:5,
>             cor=1:5,
>             zCuts=1:5
>           )
>         cols <-
>           c("#0288D1", "#039BE5",
>             "#03A9F4","#29B6F6", "#4FC3F7",
>             "#FFCDD2", "#E57373", "#F44336",
>             "#E53935", "#D32F2F", "#C62828",
>             "#B71C1C")
>         zCuts <- seq(-.5, 0.6, by=0.1)
>         filename <- "/tmp/catalin1.png"
>         width <- 800
>         height <- 600
>         res <- 150
>         png(filename=filename, width=width, height=height, res=res)
>         mapping <- ggplot2::aes(
>           x = as.factor(spei),
>           y = as.factor(month),
>           fill = cut(cor, zCuts)
>         )
>         p <- ggplot2::ggplot(df1, mapping=mapping) +
>            ggplot2::geom_tile() +
>            ggplot2::scale_fill_gradient(limits = c(-0.7, 0.7))
>         #   ggplot2::scale_fill_manual(values=cols)
>         p
>         dev.off()
>
> ... which only produces the error
>
>         Error: Discrete value supplied to continuous scale
>         Execution halted
>
> V
>
> r
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: unique scale color ggplot2

Rasmus Liland-3
On 2020-07-07 19:23 +0200, Thierry Onkelinx wrote:
>
> Don't use the cut() function.

Ah, I see it now.  Changing

        fill = cut(cor, zCuts)

to

        fill = cor

did it, probably.  Perhaps Catalin agrees.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: unique scale color ggplot2

catalin roibu
Dear all,

Thank you for your email and help. I solved the problem!

All the best!

Catalin

On Tue, 7 Jul 2020 at 20:43, Rasmus Liland <[hidden email]> wrote:

> On 2020-07-07 19:23 +0200, Thierry Onkelinx wrote:
> >
> > Don't use the cut() function.
>
> Ah, I see it now.  Changing
>
>         fill = cut(cor, zCuts)
>
> to
>
>         fill = cor
>
> did it, probably.  Perhaps Catalin agrees.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--

-
-
Catalin-Constantin ROIBU
‚Äč
Lecturer PhD, Forestry engineer
Forestry Faculty of Suceava
Str. Universitatii no. 13, Suceava, 720229, Romania
office phone      +4 0230 52 29 78, ext. 531
mobile phone    +4 0745 53 18 01
FAX:                +4 0230 52 16 64
silvic.usv.ro <http://www.usv.ro/>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.