Change colours in ggplot










2















I have this code:



 ggplot(baseline, aes(x=Group, y=Thickness, color=Group, !is.na(Thickness))) + 
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=0.5, notch=FALSE) +
geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5) +
scale_x_discrete(limits=c("HC", "Patients")) +
scale_y_continuous(breaks = seq(0.15, 0.30, by = 0.05 ))


By default, I get HC in red and Patients in blue. But I would like to get the box plot: HC in black and Patients in red.



This is the output image that I get



A reproducible example:



PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9


I can't find the code. Could you help me?



Thanks
Lili










share|improve this question



















  • 2





    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 14 '18 at 16:12











  • I am afraid I don't have any example. I just need to add a piece of code either to geom_boxplot or to geom_dotplot to change the colour, but I am unable. I've tried with this web but I am unable sthda.com/english/wiki/… Thanks for your help!

    – Lili
    Nov 14 '18 at 16:46






  • 2





    Of course you have an example - you are using a data object called baseline for your plot. If you don't take the effort to make a dummy data sample, you could use dput(head(baseline)) and post the output - have a look at the link which @Tung provided. You won't get good answers without a good question.

    – Tjebo
    Nov 14 '18 at 17:29






  • 1





    @Lili: if you fill the boxplots with red and black, wouldn't it mask all the points inside the box?

    – Tung
    Nov 15 '18 at 19:49






  • 1





    @Lili: see my answer below

    – Tung
    Nov 16 '18 at 3:34















2















I have this code:



 ggplot(baseline, aes(x=Group, y=Thickness, color=Group, !is.na(Thickness))) + 
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=0.5, notch=FALSE) +
geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5) +
scale_x_discrete(limits=c("HC", "Patients")) +
scale_y_continuous(breaks = seq(0.15, 0.30, by = 0.05 ))


By default, I get HC in red and Patients in blue. But I would like to get the box plot: HC in black and Patients in red.



This is the output image that I get



A reproducible example:



PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9


I can't find the code. Could you help me?



Thanks
Lili










share|improve this question



















  • 2





    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 14 '18 at 16:12











  • I am afraid I don't have any example. I just need to add a piece of code either to geom_boxplot or to geom_dotplot to change the colour, but I am unable. I've tried with this web but I am unable sthda.com/english/wiki/… Thanks for your help!

    – Lili
    Nov 14 '18 at 16:46






  • 2





    Of course you have an example - you are using a data object called baseline for your plot. If you don't take the effort to make a dummy data sample, you could use dput(head(baseline)) and post the output - have a look at the link which @Tung provided. You won't get good answers without a good question.

    – Tjebo
    Nov 14 '18 at 17:29






  • 1





    @Lili: if you fill the boxplots with red and black, wouldn't it mask all the points inside the box?

    – Tung
    Nov 15 '18 at 19:49






  • 1





    @Lili: see my answer below

    – Tung
    Nov 16 '18 at 3:34













2












2








2








I have this code:



 ggplot(baseline, aes(x=Group, y=Thickness, color=Group, !is.na(Thickness))) + 
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=0.5, notch=FALSE) +
geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5) +
scale_x_discrete(limits=c("HC", "Patients")) +
scale_y_continuous(breaks = seq(0.15, 0.30, by = 0.05 ))


By default, I get HC in red and Patients in blue. But I would like to get the box plot: HC in black and Patients in red.



This is the output image that I get



A reproducible example:



PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9


I can't find the code. Could you help me?



Thanks
Lili










share|improve this question
















I have this code:



 ggplot(baseline, aes(x=Group, y=Thickness, color=Group, !is.na(Thickness))) + 
geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=0.5, notch=FALSE) +
geom_dotplot(binaxis='y', stackdir='center', dotsize=0.5) +
scale_x_discrete(limits=c("HC", "Patients")) +
scale_y_continuous(breaks = seq(0.15, 0.30, by = 0.05 ))


By default, I get HC in red and Patients in blue. But I would like to get the box plot: HC in black and Patients in red.



This is the output image that I get



A reproducible example:



PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9


I can't find the code. Could you help me?



Thanks
Lili







ggplot2 colors






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:32







Lili

















asked Nov 14 '18 at 15:08









LiliLili

338




338







  • 2





    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 14 '18 at 16:12











  • I am afraid I don't have any example. I just need to add a piece of code either to geom_boxplot or to geom_dotplot to change the colour, but I am unable. I've tried with this web but I am unable sthda.com/english/wiki/… Thanks for your help!

    – Lili
    Nov 14 '18 at 16:46






  • 2





    Of course you have an example - you are using a data object called baseline for your plot. If you don't take the effort to make a dummy data sample, you could use dput(head(baseline)) and post the output - have a look at the link which @Tung provided. You won't get good answers without a good question.

    – Tjebo
    Nov 14 '18 at 17:29






  • 1





    @Lili: if you fill the boxplots with red and black, wouldn't it mask all the points inside the box?

    – Tung
    Nov 15 '18 at 19:49






  • 1





    @Lili: see my answer below

    – Tung
    Nov 16 '18 at 3:34












  • 2





    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

    – Tung
    Nov 14 '18 at 16:12











  • I am afraid I don't have any example. I just need to add a piece of code either to geom_boxplot or to geom_dotplot to change the colour, but I am unable. I've tried with this web but I am unable sthda.com/english/wiki/… Thanks for your help!

    – Lili
    Nov 14 '18 at 16:46






  • 2





    Of course you have an example - you are using a data object called baseline for your plot. If you don't take the effort to make a dummy data sample, you could use dput(head(baseline)) and post the output - have a look at the link which @Tung provided. You won't get good answers without a good question.

    – Tjebo
    Nov 14 '18 at 17:29






  • 1





    @Lili: if you fill the boxplots with red and black, wouldn't it mask all the points inside the box?

    – Tung
    Nov 15 '18 at 19:49






  • 1





    @Lili: see my answer below

    – Tung
    Nov 16 '18 at 3:34







2




2





Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

– Tung
Nov 14 '18 at 16:12





Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?

– Tung
Nov 14 '18 at 16:12













I am afraid I don't have any example. I just need to add a piece of code either to geom_boxplot or to geom_dotplot to change the colour, but I am unable. I've tried with this web but I am unable sthda.com/english/wiki/… Thanks for your help!

– Lili
Nov 14 '18 at 16:46





I am afraid I don't have any example. I just need to add a piece of code either to geom_boxplot or to geom_dotplot to change the colour, but I am unable. I've tried with this web but I am unable sthda.com/english/wiki/… Thanks for your help!

– Lili
Nov 14 '18 at 16:46




2




2





Of course you have an example - you are using a data object called baseline for your plot. If you don't take the effort to make a dummy data sample, you could use dput(head(baseline)) and post the output - have a look at the link which @Tung provided. You won't get good answers without a good question.

– Tjebo
Nov 14 '18 at 17:29





Of course you have an example - you are using a data object called baseline for your plot. If you don't take the effort to make a dummy data sample, you could use dput(head(baseline)) and post the output - have a look at the link which @Tung provided. You won't get good answers without a good question.

– Tjebo
Nov 14 '18 at 17:29




1




1





@Lili: if you fill the boxplots with red and black, wouldn't it mask all the points inside the box?

– Tung
Nov 15 '18 at 19:49





@Lili: if you fill the boxplots with red and black, wouldn't it mask all the points inside the box?

– Tung
Nov 15 '18 at 19:49




1




1





@Lili: see my answer below

– Tung
Nov 16 '18 at 3:34





@Lili: see my answer below

– Tung
Nov 16 '18 at 3:34












1 Answer
1






active

oldest

votes


















2














You can use scale_color_manual to specify desired color.
P.S: if you want a fancier hybrid boxplot, take a look at this





library(tidyverse)

df <- read_table("PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9")

ggplot(df, aes(x = Group., y = Thickness, color = Group.)) +
geom_boxplot(outlier.colour = "black",
outlier.size = 0.5, notch = FALSE) +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5,
show.legend = FALSE) +
scale_color_manual(values = c("HC" = "black", "Patient" = "red"))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.




Created on 2018-11-15 by the reprex package (v0.2.1.9000)






share|improve this answer


















  • 1





    This is amazing @Tung! thanks :)

    – Lili
    Nov 16 '18 at 15:26











  • @Done! thanks :)

    – Lili
    Nov 19 '18 at 10:46










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303253%2fchange-colours-in-ggplot%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can use scale_color_manual to specify desired color.
P.S: if you want a fancier hybrid boxplot, take a look at this





library(tidyverse)

df <- read_table("PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9")

ggplot(df, aes(x = Group., y = Thickness, color = Group.)) +
geom_boxplot(outlier.colour = "black",
outlier.size = 0.5, notch = FALSE) +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5,
show.legend = FALSE) +
scale_color_manual(values = c("HC" = "black", "Patient" = "red"))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.




Created on 2018-11-15 by the reprex package (v0.2.1.9000)






share|improve this answer


















  • 1





    This is amazing @Tung! thanks :)

    – Lili
    Nov 16 '18 at 15:26











  • @Done! thanks :)

    – Lili
    Nov 19 '18 at 10:46















2














You can use scale_color_manual to specify desired color.
P.S: if you want a fancier hybrid boxplot, take a look at this





library(tidyverse)

df <- read_table("PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9")

ggplot(df, aes(x = Group., y = Thickness, color = Group.)) +
geom_boxplot(outlier.colour = "black",
outlier.size = 0.5, notch = FALSE) +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5,
show.legend = FALSE) +
scale_color_manual(values = c("HC" = "black", "Patient" = "red"))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.




Created on 2018-11-15 by the reprex package (v0.2.1.9000)






share|improve this answer


















  • 1





    This is amazing @Tung! thanks :)

    – Lili
    Nov 16 '18 at 15:26











  • @Done! thanks :)

    – Lili
    Nov 19 '18 at 10:46













2












2








2







You can use scale_color_manual to specify desired color.
P.S: if you want a fancier hybrid boxplot, take a look at this





library(tidyverse)

df <- read_table("PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9")

ggplot(df, aes(x = Group., y = Thickness, color = Group.)) +
geom_boxplot(outlier.colour = "black",
outlier.size = 0.5, notch = FALSE) +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5,
show.legend = FALSE) +
scale_color_manual(values = c("HC" = "black", "Patient" = "red"))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.




Created on 2018-11-15 by the reprex package (v0.2.1.9000)






share|improve this answer













You can use scale_color_manual to specify desired color.
P.S: if you want a fancier hybrid boxplot, take a look at this





library(tidyverse)

df <- read_table("PatientID. Group. Thickness
1OD HC 0.5
1OS HC 0.5
23OD Patient 0.001
23OS Patient 0.01
44OD Patient 0.03
44OS Patient 0.04
3OD HC 0.7
3OS HC 0.9")

ggplot(df, aes(x = Group., y = Thickness, color = Group.)) +
geom_boxplot(outlier.colour = "black",
outlier.size = 0.5, notch = FALSE) +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5,
show.legend = FALSE) +
scale_color_manual(values = c("HC" = "black", "Patient" = "red"))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.




Created on 2018-11-15 by the reprex package (v0.2.1.9000)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 3:33









TungTung

8,79622440




8,79622440







  • 1





    This is amazing @Tung! thanks :)

    – Lili
    Nov 16 '18 at 15:26











  • @Done! thanks :)

    – Lili
    Nov 19 '18 at 10:46












  • 1





    This is amazing @Tung! thanks :)

    – Lili
    Nov 16 '18 at 15:26











  • @Done! thanks :)

    – Lili
    Nov 19 '18 at 10:46







1




1





This is amazing @Tung! thanks :)

– Lili
Nov 16 '18 at 15:26





This is amazing @Tung! thanks :)

– Lili
Nov 16 '18 at 15:26













@Done! thanks :)

– Lili
Nov 19 '18 at 10:46





@Done! thanks :)

– Lili
Nov 19 '18 at 10:46

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303253%2fchange-colours-in-ggplot%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党