0
votes

Sorry if my question is somehow confusing, don't hesitate to edit it.

I'm comparing 2 sets of data and I'm now creating plots.

On some plots, I can show both groups and ggplot automatically add some pretty colors, in my case red for the first group, and blue for the second one.

My problem is that on other plots, I have to show either one group or the other, and so the color is always red.

How can I manually ask ggplot to fill my plot with the second color of the theme ?

Here is some dummy code if you like :

df <- data.frame(f1=factor(rbinom(100, 1, 0.45), label=c("m","w")), 
                 f2=factor(rbinom(100, 1, 0.45), label=c("young","old")),
                 boxthis=rnorm(100))

#both groups on the graph, 2 colors (red and blue) :
ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + geom_boxplot() 

#each group on one graph, both the same color (and I'd like one red and one blue)
ggplot(aes(y = boxthis, x = f2, fill = "m"), data = df) + geom_boxplot()
ggplot(aes(y = boxthis, x = f2, fill = "w"), data = df) + geom_boxplot()

#I know the grouping is incorrect, this is just an example where I keep the same df
1
Wouldn't manually settings a palette or colors solve the problem?Roman Luštrik
yes, it would, but I have a lot of plots so it would be tedious.Dan Chaltiel
I dont get your question, Do you want your individual groups in blue?S Rivero
No, I want one red and one blue, the same colors that ggplot used in the first grouped plot.Dan Chaltiel
ggplot(aes(y = boxthis, x = f2, fill = "m"), data = df) + geom_boxplot(fill = '#F8766D') ggplot(aes(y = boxthis, x = f2, fill = "w"), data = df) + geom_boxplot(fill = '#00BFC4').S Rivero

1 Answers

0
votes

Are facets what you're looking for? Try this:

ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + 
    geom_boxplot() +
    facet_grid(~ f1)

Link to image