Morts81
Morts81

Reputation: 439

GGPlot2 - Colouring Specific Bars in Plot

Imagine a dataset pts_allowed as follows:

   Opp Gms    MBs     DSs Mins MBvOpp DSvOpp
24 PHO  24 5638.4 5495.00 5785   9.09   8.31
22 ORL  23 5313.1 5206.25 5520   7.73   7.54
3  BRK  22 5062.0 4944.00 5279   7.32   6.78
1  ATL  22 4933.6 4924.00 5280   4.58   6.33
4  CHI  21 4683.2 4628.75 5065   3.49   4.20
14 LAL  22 4920.5 4790.50 5330   3.33   2.48
13 LAC  21 4673.1 4582.00 5065   3.26   3.14

I plot this as per follows:

library(ggplot2)

plot <- ggplot(pts_allowed, aes(Opp, MBvOpp)) 
plot <- plot + geom_bar(stat = 'identity', fill = 'gray40')
plot <- plot + theme(text = element_text(size = 16), axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), panel.background = element_rect(fill = "aliceblue"),plot.background = element_rect(fill = "aliceblue"))
plot <- plot + theme(axis.ticks.x = element_blank(), axis.ticks.y = element_blank())
plot <- plot + theme(axis.text.x = element_text(colour = "gray40"),axis.text.y = element_text(colour = "gray40"))
plot <- plot + theme(axis.title.x = element_text(colour = "gray40"),axis.title.y = element_text(colour = "gray40"))

enter image description here

After this however, I want to specify a different colour for the bars where pts_allowed$Opp are in the vector tms:

tms <- c("PHO","LAL","BRK","CHI")

Is there a simple line of code that I can add after the fact to change the colour of those specific bars?

Upvotes: 0

Views: 56

Answers (1)

missuse
missuse

Reputation: 19756

The usual way to accomplish the desired in ggplot is to make an additional column in the data frame with the desired mapping. Example:

tms <- c("PHO","LAL","BRK","CHI")
df <- read.table(text = "   Opp Gms    MBs     DSs Mins MBvOpp DSvOpp
           24 PHO  24 5638.4 5495.00 5785   9.09   8.31
           22 ORL  23 5313.1 5206.25 5520   7.73   7.54
           3  BRK  22 5062.0 4944.00 5279   7.32   6.78
           1  ATL  22 4933.6 4924.00 5280   4.58   6.33
           4  CHI  21 4683.2 4628.75 5065   3.49   4.20
           14 LAL  22 4920.5 4790.50 5330   3.33   2.48
           13 LAC  21 4673.1 4582.00 5065   3.26   3.14", header = T)

library(tidyverse)

df %>%
  mutate(fill = as.factor(ifelse(Opp %in% tms, 1, 0))) %>% #make an additional column named fill which will be 1 if Opp is in tms and 0 otherwise. Instead of 0/1 one can specify legend key names for instance: "tms", "not tms"
  ggplot(aes(Opp, MBvOpp)) +
  geom_col(aes(fill = fill)) + #geom_col is the same as geom_bar("identity"), specify fill inside aes here.
  theme(text = element_text(size = 16),
        axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, colour = "gray40"),
        panel.background = element_rect(fill = "aliceblue"),
        plot.background = element_rect(fill = "aliceblue"),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_text(colour = "gray40"),
        axis.title.x = element_text(colour = "gray40"),
        axis.title.y = element_text(colour = "gray40")) +
scale_fill_manual(values = c("gray40", "gray80")) # to keep the grey pallete 

enter image description here

If you need it after the plot, you can add the following line of code:

plot + geom_col(fill = ifelse(df$Opp %in% tms, "gray40", "grey80"))

the result will be the same but the legend will be absent since fill was not specified in aes

if the legend is desired:

plot + 
  geom_col(aes(fill = as.factor(ifelse(Opp %in% tms, 1, 0))))+
  scale_fill_manual("fill", values = c("gray40", "gray80")) 

Upvotes: 3

Related Questions