-2
Teeth <- caries$after - caries$before
anova <- aov(Teeth~inst * treat, data=caries)
summary(anova)

ggplot(data = caries, aes(x=inst, y=Teeth, group=inst)) + 
       geom_boxplot(colour = "black", fill = "dodgerblue")

This is my code so far - caries is the imported data set.

I am trying to make the three different boxes different colors, and right now they are only in dodger blue.

Data

caries <- structure(list(subject = 1:69, inst = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
    before = c(7L, 20L, 21L, 1L, 3L, 20L, 9L, 2L, 11L, 15L, 7L, 
    17L, 9L, 1L, 3L, 10L, 13L, 3L, 4L, 4L, 15L, 6L, 4L, 18L, 
    11L, 9L, 4L, 5L, 11L, 4L, 4L, 7L, 0L, 3L, 0L, 8L, 2L, 13L, 
    9L, 15L, 13L, 2L, 9L, 4L, 10L, 7L, 14L, 7L, 3L, 9L, 8L, 19L, 
    10L, 10L, 7L, 13L, 5L, 1L, 8L, 4L, 4L, 14L, 8L, 3L, 11L, 
    16L, 8L, 0L, 3L), after = c(11L, 24L, 25L, 2L, 7L, 23L, 13L, 
    4L, 13L, 18L, 10L, 17L, 11L, 5L, 7L, 14L, 17L, 4L, 7L, 9L, 
    18L, 8L, 6L, 19L, 12L, 9L, 7L, 7L, 14L, 6L, 4L, 7L, 4L, 3L, 
    1L, 8L, 4L, 18L, 12L, 18L, 17L, 5L, 12L, 6L, 14L, 11L, 15L, 
    10L, 6L, 12L, 10L, 19L, 13L, 12L, 11L, 12L, 8L, 3L, 9L, 5L, 
    7L, 14L, 10L, 5L, 12L, 18L, 8L, 1L, 4L), treat = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 
    3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("APF", "SF", 
    "W"), class = "factor")), .Names = c("subject", "inst", "before", 
"after", "treat"), class = "data.frame", row.names = c(NA, -69L
))

https://pastebin.com/MeRw69ky <

user20650
  • 24,654
  • 5
  • 56
  • 91
  • 2
    To make a [MCVE](https://stackoverflow.com/help/mcve) you either need to provide your data (e.g., using `dput`) or provide an example using a publicly available data set. – Dan Apr 22 '18 at 21:38
  • Hello! thank you! I am new on the site and this is my first post, I am trying to figure out how to use dput – Kristen Felohz Apr 22 '18 at 21:45
  • Welcome to SO. Please have a look through the site orientation and help sections. Also, please have a read of [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/). There is a general expectation on this site that before posting a question, you should do a reasonable amount of research of your own. There are so many resource to answer your question that a simple google search for 'color in ggplot' would probably be enough. – dww Apr 22 '18 at 22:02
  • Hi! thank you for these resources. I've tried 5 variations of code and keep getting an error – Kristen Felohz Apr 22 '18 at 22:09
  • ggplot(data = caries, aes(x=inst, y=Teeth, group=inst)) + geom_boxplot(bp + scale_fill_manual(values=c("red", "dodgerblue", "green")) Error: unexpected symbol in: "ggplot(data = caries, aes(x=inst, y=Teeth, group=inst)) + geom_boxplot(scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) ggplot" > bp + scale_fill_manual(breaks = c("2", "1", "0.5"), + values=c("red", "blue", "green")) Error: object 'bp' not found > ggplot(data = caries, aes(x=inst, y=Teeth, fill=inst)) + geom_boxplot(scale_fill_hue(l=40, c=35)) Error: Mapping must be created by `aes()` or `aes_()` – Kristen Felohz Apr 22 '18 at 22:10

2 Answers2

0

In order to have a variable determine the colors in your plot, you have to map that variable to color in an aes call. Here's an example with the mpg dataset. I'd suggest reading through the ggplot2 docs, because there are a ton of examples there, including at least one that's basically the same as this one.

library(ggplot2)

ggplot(mpg, aes(x = class, y = hwy, color = class)) +
    geom_boxplot()

Created on 2018-04-22 by the reprex package (v0.2.0).

camille
  • 16,432
  • 18
  • 38
  • 60
0

I have fixed it with this code. thanks everybody for your help!

ggplot(caries, aes(inst, Teeth, group=inst, fill = inst)) + geom_boxplot()

ggplot(caries, aes(treat, Teeth, group=treat, fill = treat)) + geom_boxplot()
Dan
  • 11,370
  • 4
  • 43
  • 68