0

I would like to create four boxplots next to each other. They should all filled with another color. I tried: scale_fill_manual(values=c("1"="red", "2"="green", "3"="yellow", "4"="blue"))

options(stringsAsFactors = FALSE) 
input <- "C:\\statistical_tests\\boxplot.csv" 
boxplot<- read.csv(input, sep=";") 

library(ggplot2) 
library(scales) 
means <- aggregate(number ~ CPOD, paper, mean) 
p <- ggplot(boxplot, aes(group=time,y=number, x=as.character(time))) +
     geom_boxplot()+ theme_bw() + 
     scale_fill_manual(values=c("1"="red", "2"="green", "3"="yellow", "4"="blue"))+ 
             panel.grid.minor=element_line(colour="white"), 
             axis.text.x = element_text(size=8, colour="black"),
             axis.text.y=element_text(size=8, colour="black")) 
print(p) 

Unfortunately, it is not working. Could someone help me? Thank you very much!

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Dolphin94
  • 319
  • 1
  • 2
  • 8
  • 1
    https://stackoverflow.com/questions/8320462/ggplot2-how-to-adjust-fill-colour-in-a-boxplot-and-change-legend-text – Edward Apr 01 '20 at 09:49
  • 2
    Does this answer your question? [ggplot2: How to adjust fill colour in a boxplot (and change legend text)?](https://stackoverflow.com/questions/8320462/ggplot2-how-to-adjust-fill-colour-in-a-boxplot-and-change-legend-text) – Scipione Sarlo Apr 01 '20 at 09:55

1 Answers1

0

Could this be what you want?

data("iris")
ggplot( iris, aes( x = Species, y = Sepal.Length, fill = Species ) ) +
  geom_boxplot()

Beware that this works only if the x-axis variable is either a factor or a character. You can define it in the data.frame or you can a more on-the-fly approach as shown below. This is useful when you don't want to change the structure of your original data.

data("iris")
ggplot( iris, aes( x = as.factor(Species), y = Sepal.Length, fill = Species ) ) +
  geom_boxplot()

enter image description here

Francesco Grossetti
  • 1,555
  • 9
  • 17
  • Thanks, but it is not working. I have in one column "number" and in the other column time (from 1 to 4). Now I would like to create 4 boxplots: time on the x-axis and number on the y-axis. Each of the 4 boxplots should have another color. I tried: scale_fill_manual(values=c("1"="red", "2"="green", "3"="yellow", "4"="blue") and scale_fill_manual(values=c(red", "green", "yellow", "blue") – Dolphin94 Apr 01 '20 at 11:20
  • I don't understand. Do you want a single plot with 4 box plots in it, or do you want to have 4 independent plot? – Francesco Grossetti Apr 01 '20 at 11:29
  • I would like to create 4 boxplots in one figure (like the figure, which you have sent) – Dolphin94 Apr 01 '20 at 11:39
  • Then why my example does not work? All you want is to have a factor variable on x-axis and your outcome variable as y-axis. You then color each box plot by simply putting `fill = time` within `aes()`. Something like `ggplot(data, aes(x = time, y = number, fill = time) +geom_boxplot()` – Francesco Grossetti Apr 01 '20 at 11:44
  • It says: "Error: Continuous value supplied to discrete scale" – Dolphin94 Apr 01 '20 at 11:50
  • There you go. You want to convert `time` to factor. This trick doesn't work if you have a continuous variable (i.e. `numeric`). I will update my answer above to adjust for this. – Francesco Grossetti Apr 01 '20 at 11:52