STHDA forum

Plotting, data visualizations » ggplot2: Changing geom_jitter in specific colors

Slingshot booster

rank_0.png

Avatar

Signed up since: 31/08/2017

Messages: 2

On 08/31/2017 at 16h28
I would like to change the color of the geom_jitter into the same colors as the boxplot, but whatever I do, I can't get it right. Maybe someone could help me with this? I have the following data:

Code R :
str(data)
'data.frame':    407 obs. of  3 variables:
$ Country                       : Factor w/ 2 levels "FR","NZ": 2 2 2 2 2 2 2 2 2 2 ...
$ Status                        : Factor w/ 2 levels "Voor","Na": 1 1 1 2 2 2 2 2 2 2 ...
$ Treatment.succes              : num  89.3 89 89.7 80 88.7 ...
 
cbPalette=c("forestgreen","gold1")
 
ggplot(data=subset(data, aes(x=Status, y=Treatment.succes, fill=Status))+
  geom_boxplot(outlier.shape = NA) +  
  geom_jitter(alpha=0.9)+
  scale_fill_manual(values=cbPalette)+
  theme_bw()+
  facet_grid(.~Country)+   
  labs(x = "\nVoor of na Treatment\n", y = "\nTreatment effect (%)\n") +
  theme(strip.text.x = element_text(size=12, color="black", face="bold"),
        axis.text=element_text(size=12),
        legend.text=element_text(size=15),
        legend.title = element_text(size=15, face = "bold"),
        legend.position = "bottom",
        axis.text.x=element_text(angle=45,hjust=1),
        axis.title=element_text(size=15))



It gives me:




But how do I change the color of the geom_jitter...?
   

Administrator

rank_admin.png

Avatar

Signed up since: 10/02/2013

Messages: 32

On 08/31/2017 at 18h19
Specifying the color mapping in geom_jitter() should work. For example, try this:

Code R :
 
data <- ToothGrowth
data$dose <- as.factor(data$dose)
ggplot(data, aes(x = dose, y = len)) +
  geom_boxplot(aes(fill = dose), alpha = 0.5)+
  geom_jitter(aes(color = dose))
 


The resulting plot looks like this:

Website    

Slingshot booster

rank_0.png

Avatar

Signed up since: 31/08/2017

Messages: 2

On 09/01/2017 at 08h49
unfortunately that doesn't work. The thing is that I want the boxsplot in specific colours and not the standard pink/blue. If I ad the aes(fill = Status), it just changes the fill colour of the boxplot to pink/blue, the jitter stays the same..
   

Administrator

rank_admin.png

Avatar

Signed up since: 10/02/2013

Messages: 32

On 09/01/2017 at 09h29
You should use also aes(color = Status) for jitter points, like in the following code:

Code R :
 
data <- ToothGrowth
data$dose <- as.factor(data$dose)
cbPalette=c("forestgreen","gold1", "darkred")
ggplot(data, aes(x = dose, y = len)) +
  geom_boxplot(aes(fill = dose), alpha = 0.5)+
  geom_jitter(aes(color = dose))+
  scale_fill_manual(values=cbPalette)+ # Boxplot fill color
  scale_color_manual(values = cbPalette) # Jitter color palette
 


Website    
Answer
You aren't allowed to write in this category
1 User online : 0 Administrator, 0 Moderator, 0 Member and 1 Visitor
User online : No member online