ggplot 3-color gradient scales

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

ggplot 3-color gradient scales

April Ettington
Currently I am using these settings in ggplot to make a gradient from red
to blue.

geom_point( aes(x, y, color=z) ) +
scale_colour_gradient(low = "red",high = "blue") +

z is a ratio, and currently I am able to identify which have high and low
values, but I'd really like to be able to distinguish which are >1, <1, or
close to 1 by color.  It would be great if I could set a middle color in
this gradient (eg. green) that is set the the value of 1, even if that is
not the exact midpoint between my highest and lowest values.  Is there a
way to do this in R?

Thank you,
April

        [[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: ggplot 3-color gradient scales

Jeff Newmiller
Check out scale_colour_gradient2()

On August 23, 2020 8:12:06 PM PDT, April Ettington <[hidden email]> wrote:

>Currently I am using these settings in ggplot to make a gradient from
>red
>to blue.
>
>geom_point( aes(x, y, color=z) ) +
>scale_colour_gradient(low = "red",high = "blue") +
>
>z is a ratio, and currently I am able to identify which have high and
>low
>values, but I'd really like to be able to distinguish which are >1, <1,
>or
>close to 1 by color.  It would be great if I could set a middle color
>in
>this gradient (eg. green) that is set the the value of 1, even if that
>is
>not the exact midpoint between my highest and lowest values.  Is there
>a
>way to do this in R?
>
>Thank you,
>April
>
> [[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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: ggplot 3-color gradient scales

Rui Barradas
Hello,

Note that the midpoint argument can make a big difference. In the code
below try commenting out the line where the default is changed.


f <- function(x){
   (x - min(x))/(max(x) - min(x))
}

library(ggplot2)

df1 <- iris[3:5]
names(df1)[1:2] <- c("x", "y")
df1$z <- ave(df1$y, df1$Species, FUN = f)

ggplot(df1) +
   geom_point( aes(x, y, color = z) ) +
   scale_color_gradient2(low = "red",
                         mid = "yellow",
                         high = "blue",
                         midpoint = 0.5
                         )

Hope this helps,

Rui Barradas


Às 04:43 de 24/08/20, Jeff Newmiller escreveu:

> Check out scale_colour_gradient2()
>
> On August 23, 2020 8:12:06 PM PDT, April Ettington <[hidden email]> wrote:
>> Currently I am using these settings in ggplot to make a gradient from
>> red
>> to blue.
>>
>> geom_point( aes(x, y, color=z) ) +
>> scale_colour_gradient(low = "red",high = "blue") +
>>
>> z is a ratio, and currently I am able to identify which have high and
>> low
>> values, but I'd really like to be able to distinguish which are >1, <1,
>> or
>> close to 1 by color.  It would be great if I could set a middle color
>> in
>> this gradient (eg. green) that is set the the value of 1, even if that
>> is
>> not the exact midpoint between my highest and lowest values.  Is there
>> a
>> way to do this in R?
>>
>> Thank you,
>> April
>>
>> [[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.
>

______________________________________________
[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: ggplot 3-color gradient scales

April Ettington
Thank you so much!


On Mon, Aug 24, 2020 at 5:33 PM Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Note that the midpoint argument can make a big difference. In the code
> below try commenting out the line where the default is changed.
>
>
> f <- function(x){
>    (x - min(x))/(max(x) - min(x))
> }
>
> library(ggplot2)
>
> df1 <- iris[3:5]
> names(df1)[1:2] <- c("x", "y")
> df1$z <- ave(df1$y, df1$Species, FUN = f)
>
> ggplot(df1) +
>    geom_point( aes(x, y, color = z) ) +
>    scale_color_gradient2(low = "red",
>                          mid = "yellow",
>                          high = "blue",
>                          midpoint = 0.5
>                          )
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 04:43 de 24/08/20, Jeff Newmiller escreveu:
> > Check out scale_colour_gradient2()
> >
> > On August 23, 2020 8:12:06 PM PDT, April Ettington <
> [hidden email]> wrote:
> >> Currently I am using these settings in ggplot to make a gradient from
> >> red
> >> to blue.
> >>
> >> geom_point( aes(x, y, color=z) ) +
> >> scale_colour_gradient(low = "red",high = "blue") +
> >>
> >> z is a ratio, and currently I am able to identify which have high and
> >> low
> >> values, but I'd really like to be able to distinguish which are >1, <1,
> >> or
> >> close to 1 by color.  It would be great if I could set a middle color
> >> in
> >> this gradient (eg. green) that is set the the value of 1, even if that
> >> is
> >> not the exact midpoint between my highest and lowest values.  Is there
> >> a
> >> way to do this in R?
> >>
> >> Thank you,
> >> April
> >>
> >>      [[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: ggplot 3-color gradient scales

April Ettington
Is there a way to set it to 3 color categories instead of a gradient?  Like
if the color is based on the numbers in a dataframe column, can I make it
so anything >1.2 is red, <0.8 is blue, and anything in the middle is green?


On Mon, Aug 24, 2020 at 6:28 PM April Ettington <[hidden email]>
wrote:

> Thank you so much!
>
>
> On Mon, Aug 24, 2020 at 5:33 PM Rui Barradas <[hidden email]> wrote:
>
>> Hello,
>>
>> Note that the midpoint argument can make a big difference. In the code
>> below try commenting out the line where the default is changed.
>>
>>
>> f <- function(x){
>>    (x - min(x))/(max(x) - min(x))
>> }
>>
>> library(ggplot2)
>>
>> df1 <- iris[3:5]
>> names(df1)[1:2] <- c("x", "y")
>> df1$z <- ave(df1$y, df1$Species, FUN = f)
>>
>> ggplot(df1) +
>>    geom_point( aes(x, y, color = z) ) +
>>    scale_color_gradient2(low = "red",
>>                          mid = "yellow",
>>                          high = "blue",
>>                          midpoint = 0.5
>>                          )
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>>
>> Às 04:43 de 24/08/20, Jeff Newmiller escreveu:
>> > Check out scale_colour_gradient2()
>> >
>> > On August 23, 2020 8:12:06 PM PDT, April Ettington <
>> [hidden email]> wrote:
>> >> Currently I am using these settings in ggplot to make a gradient from
>> >> red
>> >> to blue.
>> >>
>> >> geom_point( aes(x, y, color=z) ) +
>> >> scale_colour_gradient(low = "red",high = "blue") +
>> >>
>> >> z is a ratio, and currently I am able to identify which have high and
>> >> low
>> >> values, but I'd really like to be able to distinguish which are >1, <1,
>> >> or
>> >> close to 1 by color.  It would be great if I could set a middle color
>> >> in
>> >> this gradient (eg. green) that is set the the value of 1, even if that
>> >> is
>> >> not the exact midpoint between my highest and lowest values.  Is there
>> >> a
>> >> way to do this in R?
>> >>
>> >> Thank you,
>> >> April
>> >>
>> >>      [[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: ggplot 3-color gradient scales

PIKAL Petr
Hi

Maybe scale_colour_manual?

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of April Ettington
> Sent: Tuesday, August 25, 2020 11:39 AM
> To: Rui Barradas <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] ggplot 3-color gradient scales
>
> Is there a way to set it to 3 color categories instead of a gradient?  Like if the
> color is based on the numbers in a dataframe column, can I make it so
> anything >1.2 is red, <0.8 is blue, and anything in the middle is green?
>
>
> On Mon, Aug 24, 2020 at 6:28 PM April Ettington <[hidden email]>
> wrote:
>
> > Thank you so much!
> >
> >
> > On Mon, Aug 24, 2020 at 5:33 PM Rui Barradas <[hidden email]>
> wrote:
> >
> >> Hello,
> >>
> >> Note that the midpoint argument can make a big difference. In the
> >> code below try commenting out the line where the default is changed.
> >>
> >>
> >> f <- function(x){
> >>    (x - min(x))/(max(x) - min(x))
> >> }
> >>
> >> library(ggplot2)
> >>
> >> df1 <- iris[3:5]
> >> names(df1)[1:2] <- c("x", "y")
> >> df1$z <- ave(df1$y, df1$Species, FUN = f)
> >>
> >> ggplot(df1) +
> >>    geom_point( aes(x, y, color = z) ) +
> >>    scale_color_gradient2(low = "red",
> >>                          mid = "yellow",
> >>                          high = "blue",
> >>                          midpoint = 0.5
> >>                          )
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >>
> >> Às 04:43 de 24/08/20, Jeff Newmiller escreveu:
> >> > Check out scale_colour_gradient2()
> >> >
> >> > On August 23, 2020 8:12:06 PM PDT, April Ettington <
> >> [hidden email]> wrote:
> >> >> Currently I am using these settings in ggplot to make a gradient
> >> >> from red to blue.
> >> >>
> >> >> geom_point( aes(x, y, color=z) ) + scale_colour_gradient(low =
> >> >> "red",high = "blue") +
> >> >>
> >> >> z is a ratio, and currently I am able to identify which have high
> >> >> and low values, but I'd really like to be able to distinguish
> >> >> which are >1, <1, or close to 1 by color.  It would be great if I
> >> >> could set a middle color in this gradient (eg. green) that is set
> >> >> the the value of 1, even if that is not the exact midpoint between
> >> >> my highest and lowest values.  Is there a way to do this in R?
> >> >>
> >> >> Thank you,
> >> >> April
> >> >>
> >> >>      [[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.

______________________________________________
[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: ggplot 3-color gradient scales

Rui Barradas
In reply to this post by April Ettington
Hello,

If you want a predetermined number of colors, discretise the data and
use scale_color_manual. In the code below I first compute another vector
z, with a different range, 0 to 2. (In my first mail it was 0 to 1.)

g <- function(x, a = 0, b = 1){
   (b - a)*(x - min(x))/(max(x) - min(x)) + a
}

library(ggplot2)

df1 <- iris[3:5]
names(df1)[1:2] <- c("x", "y")
df1$z <- ave(df1$y, df1$Species, FUN = function(x) g(x, a = 0, b = 2))


Now is the step that solves the problem, to bin the vector. Other
options could include findInterval. Then the two plot instructions are
equivalent.

df1$z <- cut(df1$z,
              breaks = c(-Inf, 0.8, 1.2, Inf),
              labels = c("Small", "Medium", "Large"))


ggplot(df1) +
   geom_point( aes(x, y, color = z) ) +
   scale_color_manual(values = c("red", "green", "blue"))

ggplot(df1) +
   geom_point( aes(x, y, color = z) ) +
   scale_color_manual(breaks = c("Small", "Medium", "Large"),
                      values = c("Small" = "red", "Medium" = "green",
"Large" = "blue"))


Hope this helps,

Rui Barradas


Às 10:38 de 25/08/20, April Ettington escreveu:

> Is there a way to set it to 3 color categories instead of a gradient?  
> Like if the color is based on the numbers in a dataframe column, can I
> make it so anything >1.2 is red, <0.8 is blue, and anything in the
> middle is green?
>
>
> On Mon, Aug 24, 2020 at 6:28 PM April Ettington
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Thank you so much!
>
>
>     On Mon, Aug 24, 2020 at 5:33 PM Rui Barradas <[hidden email]
>     <mailto:[hidden email]>> wrote:
>
>         Hello,
>
>         Note that the midpoint argument can make a big difference. In
>         the code
>         below try commenting out the line where the default is changed.
>
>
>         f <- function(x){
>             (x - min(x))/(max(x) - min(x))
>         }
>
>         library(ggplot2)
>
>         df1 <- iris[3:5]
>         names(df1)[1:2] <- c("x", "y")
>         df1$z <- ave(df1$y, df1$Species, FUN = f)
>
>         ggplot(df1) +
>             geom_point( aes(x, y, color = z) ) +
>             scale_color_gradient2(low = "red",
>                                   mid = "yellow",
>                                   high = "blue",
>                                   midpoint = 0.5
>                                   )
>
>         Hope this helps,
>
>         Rui Barradas
>
>
>         Às 04:43 de 24/08/20, Jeff Newmiller escreveu:
>          > Check out scale_colour_gradient2()
>          >
>          > On August 23, 2020 8:12:06 PM PDT, April Ettington
>         <[hidden email] <mailto:[hidden email]>> wrote:
>          >> Currently I am using these settings in ggplot to make a
>         gradient from
>          >> red
>          >> to blue.
>          >>
>          >> geom_point( aes(x, y, color=z) ) +
>          >> scale_colour_gradient(low = "red",high = "blue") +
>          >>
>          >> z is a ratio, and currently I am able to identify which have
>         high and
>          >> low
>          >> values, but I'd really like to be able to distinguish which
>         are >1, <1,
>          >> or
>          >> close to 1 by color.  It would be great if I could set a
>         middle color
>          >> in
>          >> this gradient (eg. green) that is set the the value of 1,
>         even if that
>          >> is
>          >> not the exact midpoint between my highest and lowest
>         values.  Is there
>          >> a
>          >> way to do this in R?
>          >>
>          >> Thank you,
>          >> April
>          >>
>          >>      [[alternative HTML version deleted]]
>          >>
>          >> ______________________________________________
>          >> [hidden email] <mailto:[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.
>          >
>

______________________________________________
[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.