0
votes

A very similar question to the one asked here. However, in that situation the fill parameter for the two plots are different. For my situation the fill parameter is the same for both plots, but I want different color schemes.

I would like to manually change the color in the boxplots and the scatter plots (for example making the boxes white and the points colored).

Example:

require(dplyr)
require(ggplot2)

n<-4*3*10
myvalues<- rexp((n))
days    <- ntile(rexp(n),4)
doses   <- ntile(rexp(n), 3)

test    <- data.frame(values =myvalues, 
                      day = factor(days,   levels = unique(days)),
                      dose = factor(doses, levels = unique(doses)))

p<- ggplot(data = test, aes(x = day, y = values)) + 
    geom_boxplot( aes(fill = dose))+
    geom_point(   aes(fill = dose), alpha = 0.4, 
    position = position_jitterdodge())

produces a plot like this: enter image description here

Using 'scale_fill_manual()' overwrites the aesthetic on both the boxplot and the scatterplot.

I have found a hack by adding 'colour' to geom_point and then when I use scale_fill_manual() the scatter point colors are not changed:

p<- ggplot(data = test, aes(x = day, y = values)) + 
    geom_boxplot(aes(fill = dose), outlier.shape = NA)+
    geom_point(aes(fill = dose, colour = factor(test$dose)),
             position = position_jitterdodge(jitter.width = 0.1))+
    scale_fill_manual(values = c('white', 'white', 'white'))

This is What I want

Are there more efficient ways of getting the same result?

1

1 Answers

1
votes

You can use group to set the different boxplots. No need to set the fill and then overwrite it:

ggplot(data = test, aes(x = day, y = values)) + 
    geom_boxplot(aes(group = interaction(day, dose)), outlier.shape = NA)+
    geom_point(aes(fill = dose, colour = dose),
             position = position_jitterdodge(jitter.width = 0.1))

And you should never use data$column inside aes - just use the bare column. Using data$column will work in simple cases, but will break whenever there are stat layers or facets.