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

Lili
  • 547
  • 6
  • 19
  • 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`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – 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 http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually 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
  • Dear @Tjebo, thank you for your comments and I am sorry for not have provided you with the right information. I've edited the post. I hope this is useful now and clear enough. Thanks – Lili Nov 15 '18 at 14:33
  • Dear @Tung, thank you for your comments and I am sorry for not have provided you with the right information. I've edited the post. I hope this is useful now and clear enough. Thanks – Lili Nov 15 '18 at 14:33
  • 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
  • @Tung I want to change the colour of the lines, not to fill the box plots :) – Lili Nov 16 '18 at 00:05
  • 1
    @Lili: see my answer below – Tung Nov 16 '18 at 03:34

1 Answers1

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)

Tung
  • 26,371
  • 7
  • 91
  • 115