Colors on box plots in ggplot

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

Colors on box plots in ggplot

kydaviddoyle
Hello,

I'm trying to set different boxes to different colors the following page
shows
http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization


I've tried the code
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# Basic box plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len))  +
  geom_boxplot()
p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
p

But still can not get the colors to show up.  I'm sure it is something
simple I'm doing wrong and would appreciate help.

Thank you in advance
David

        [[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.
Take Care

David Doyle
Reply | Threaded
Open this post in threaded view
|

Re: Colors on box plots in ggplot

Jeff Newmiller
I see you creating a variable p, evaluating and printing a modified version of that variable, and then printing that variable (presumably overwriting the first plot). Are you executing your code one line at a time when troubleshooting?

On January 17, 2019 2:32:51 PM PST, David Doyle <[hidden email]> wrote:

>Hello,
>
>I'm trying to set different boxes to different colors the following
>page
>shows
>http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization
>
>
>I've tried the code
>ToothGrowth$dose <- as.factor(ToothGrowth$dose)
>head(ToothGrowth)
>library(ggplot2)
># Basic box plot
>p <- ggplot(ToothGrowth, aes(x=dose, y=len))  +
>  geom_boxplot()
>p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
>p
>
>But still can not get the colors to show up.  I'm sure it is something
>simple I'm doing wrong and would appreciate help.
>
>Thank you in advance
>David
>
> [[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: Colors on box plots in ggplot

S Ellison-2
In reply to this post by kydaviddoyle


> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of David
> I'm trying to set different boxes to different colors the following page
> shows


> http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-
> software-and-data-visualization
>
>
> I've tried the code
> ToothGrowth$dose <- as.factor(ToothGrowth$dose)
> head(ToothGrowth)
> library(ggplot2)
> # Basic box plot
> p <- ggplot(ToothGrowth, aes(x=dose, y=len))  +
>   geom_boxplot()
> p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
> p

You have not mapped an aesthetic to colour, so the scale (which applies to an aesthetic) is not being used at all.

Try    
( p <- ggplot(ToothGrowth, aes(x=dose, y=len))  + geom_boxplot(aes(colour=dose)) )

which uses default colours. Once you have an aes  mapping you can change the scale, so
 (     p + scale_colour_manual(values = c("red", "blue", "green")) )

gives you the colour ordering you want.

(     p + scale_colour_manual(values = c("red", "blue", "green"), guide=FALSE) )
also removes the redundant colour key.



S Ellison



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

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