user27184365
user27184365

Reputation: 21

How to color y-axis labels with user-defined color which are in different strips?

I have a dataframe like this...

dput(FCXM_AA_sp)
structure(list(Specificity_uniqueaa = structure(1:54, levels = c("A3_D161", 
"A*30:01_W152", "B57_V97", "Cw5_K138", "Cw9_R91", "B13_L145", 
"A*11:02_K19", "B18_G30", "A2_H74", "B63_F4", "B44_V199", "Cw16_L193", 
"DR8_L74", "DQ4_D71", "A80_S31", "A80_E56", "A80_N74", "A80_K253", 
"A80_K255", "Cw1_K6", "Cw1_C99", "Cw1_M248", "Cw7_L194", "Cw7_M261", 
"Cw7_S273", "DR7_K14", "DR7_Q25", "DR7_L30", "DR10_E10", "DR10_R30", 
"DR10_V31", "DQ2_S28", "DQ2_S30", "DQ2_I37", "DQ2_L52", "DQB1*06:01_P3", 
"DQB1*06:01_L9", "DR4_H13", "DR4_H33", "DR4_Y96", "DR51_Q9", 
"DR51_Q191", "DR53_A11", "DR53_C13", "DR53_L18", "DR53_W25", 
"DR53_N26", "DR53_I28", "DR53_N41", "DR53_Q48", "DR53_Y81", "DR53_Q187", 
"DQ5_L14", "DQ5_S125"), class = "factor"), AA = structure(c(15L, 
7L, 3L, 19L, 18L, 5L, 19L, 10L, 12L, 8L, 3L, 5L, 5L, 15L, 9L, 
17L, 14L, 19L, 19L, 19L, 2L, 4L, 5L, 4L, 9L, 19L, 16L, 5L, 17L, 
18L, 3L, 9L, 9L, 6L, 5L, 11L, 5L, 12L, 12L, 13L, 16L, 16L, 1L, 
2L, 5L, 7L, 14L, 6L, 14L, 16L, 13L, 16L, 5L, 9L), levels = c("A", 
"C", "V", "M", "L", "I", "W", "F", "S", "G", "P", "H", "Y", "N", 
"D", "Q", "E", "R", "K"), class = "factor"), FCXM = c("Negative", 
"Negative", "Positive", "Negative", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Negative", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Negative", "Negative", 
"Positive", "Positive", "Positive", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Negative", "Negative", "Negative", 
"Negative", "Negative", "Negative", "Negative", "Negative"), 
    FCXM_no = c(-1L, -1L, 1L, -1L, -1L, -1L, -1L, 1L, 1L, -1L, 
    -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 1L, 1L, 1L, 
    -1L, -1L, -1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, -1L, 
    -1L, 1L, 1L, 1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, -1L, 
    -1L, -1L, -1L, -1L, -1L, -1L), splits = structure(c(3L, 1L, 
    1L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L, 
    3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 3L, 3L, 1L, 2L, 
    2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 
    1L, 3L, 3L, 2L, 3L, 1L, 2L), levels = c("Hydrophobic", "Neutral", 
    "Hydrophilic"), class = "factor"), colors = structure(c(7L, 
    3L, 1L, 5L, 5L, 1L, 5L, 1L, 5L, 3L, 1L, 1L, 1L, 7L, 4L, 7L, 
    6L, 5L, 5L, 5L, 2L, 2L, 1L, 2L, 4L, 5L, 6L, 1L, 7L, 5L, 1L, 
    4L, 4L, 1L, 1L, 1L, 1L, 5L, 5L, 3L, 6L, 6L, 1L, 2L, 1L, 3L, 
    6L, 1L, 6L, 6L, 3L, 6L, 1L, 4L), levels = c("#1B04AC", "#CCECFF", 
    "#CC99FF", "#89F88B", "#EC1504", "#CCA504", "#FFCC00"), class = "factor")), row.names = c(NA, 
-54L), class = "data.frame")

I want to color the y-axis letters as mentioned in the code below. Without facet_grid it is working but when I used facet_grid it will not give the desired output as I want the exactly colors as described in code.

I have used the following code..

cat(paste(shQuote(unique(FCXM_AA_sp$AA), type="cmd"), collapse=", "))
FCXM_AA_sp$AA <- factor(FCXM_AA_sp$AA,                 # Relevel group factor
                                          levels = c("A", "C", "V", "M", "L", "I", "W", "F",  "S", "G", "P", "H", "Y",  "N", "D", "Q", "E", "R", "K"))
dput(FCXM_AA_sp)

FCXM_AA_sp$splits <- factor(FCXM_AA_sp$AA, labels=c("Hydrophobic","Hydrophobic","Hydrophobic","Hydrophobic","Hydrophobic","Hydrophobic","Hydrophobic","Hydrophobic",
                                                    "Neutral","Neutral","Neutral","Neutral","Neutral",
                                                    "Hydrophilic","Hydrophilic","Hydrophilic","Hydrophilic","Hydrophilic","Hydrophilic"))

FCXM_AA_sp$colors <- factor(FCXM_AA_sp$AA, labels=c("#1B04AC","#CCECFF","#1B04AC","#CCECFF","#1B04AC","#1B04AC","#CC99FF","#CC99FF",
                                                    "#89F88B","#1B04AC","#1B04AC","#EC1504","#CC99FF",
                                                    "#CCA504","#FFCC00","#CCA504","#FFCC00","#EC1504","#EC1504")) 

library(grid)
library(ggtext)
library(tidyverse)
library(glue)
library(ggh4x)

ggplot(FCXM_AA_sp, aes(x = FCXM_no, y = AA, fill = FCXM)) + # Fill column
  geom_bar(stat = "identity", width = .6, color = "white", size = 0.5) + # draw the bars
  labs(x = "", y = "Amino Acids") +
  geom_text(aes(label = Specificity_uniqueaa), size = 10 / .pt, position = position_stack(vjust = .5), color = "white") +
  geom_vline(xintercept=0, linetype="solid", color = "black", size=1.5) +
  facet_grid(splits~., switch = "y", scales = "free_y", space = "free_y")+ ##splits+colors # for adding two strips
  theme(strip.placement = "outside",
        strip.text.y = element_text(colour="black"),
        strip.background.y = element_rect(color="white", fill="grey", size=1.5, linetype="solid"))+
  scale_fill_brewer(palette = "Dark2") +
  #scale_fill_manual(values = c("Negative" = "darkgreen", "Positive" = "#EC1504")) +
  
    theme(strip.text.y=element_text(size=14, face="bold", angle=90, hjust=0.5, vjust=0.5),
        legend.text = element_text(colour= "black", size=18, face="bold"),
        legend.title=element_text(size=22, face="bold"), 
        plot.title = element_text(hjust = .5, size=18, face="bold"), 
        axis.ticks = element_blank(), #size=0.25
        axis.text.y = element_text(colour=c("#1B04AC","#CCECFF","#1B04AC","#CCECFF","#1B04AC","#1B04AC","#CC99FF","#CC99FF",
                                            "#89F88B","#1B04AC","#1B04AC","#EC1504","#CC99FF",
                                            "#CCA504","#FFCC00","#CCA504","#FFCC00","#EC1504","#EC1504"), size = 18, face = "bold", hjust=0.5), #library(ggh4x)

        #axis.title.x = element_text(margin = margin(t = 10), size = 28, face = "bold", color = "black"), 
        axis.title.y = element_text(margin = margin(r = 10), size = 28, face = "bold", color = "black")) # Centre plot title

Using this code I got an output as attached in the figure..

enter image description here

So, first I want the output of the graph with colored y-axis (AA) labels ("A", "C", "V", "M", "L", "I", "W", "F", "S", "G", "P", "H", "Y", "N", "D", "Q", "E", "R", "K") as (#1B04AC","#CCECFF","#1B04AC","#CCECFF","#1B04AC","#1B04AC","#CC99FF","#CC99FF","#89F88B","#1B04AC","#1B04AC","#EC1504","#CC99FF","#CCA504","#FFCC00","#CCA504","#FFCC00","#EC1504","#EC1504").

Second, I want to change the color of strip text (Hydrophobic, Neutral, Hydrophilic) as (Red, Green, Yellow) as well as their background color as (grey, blue, pink).

Upvotes: 2

Views: 69

Answers (1)

stefan
stefan

Reputation: 125478

Instead of passing a vector of colors via the theme an easier and less error prone way to achieve your desired result would be to use ggtexts capabilities to style text using some HTML and CSS and using ggtext::element_markdown instead of element_text.

EDIT: For the second part you can switch to ggh4x::facet_grid2 which via the strip= argument allows to set both the background fill and the text color for the strips.

library(ggtext)
library(tidyverse)
library(glue)
library(ggh4x)

FCXM_AA_sp$AA <- glue::glue(
  "<span style='color: {FCXM_AA_sp$colors}'>{FCXM_AA_sp$AA}</span>"
)

ggplot(FCXM_AA_sp, aes(x = FCXM_no, y = AA, fill = FCXM)) + # Fill column
  geom_bar(stat = "identity", width = .6, color = "white", size = 0.5) + # draw the bars
  labs(x = "", y = "Amino Acids") +
  geom_text(aes(label = Specificity_uniqueaa),
    size = 10 / .pt,
    position = position_stack(vjust = .5), color = "white"
  ) +
  geom_vline(xintercept = 0, linetype = "solid", color = "black", size = 1.5) +
  ggh4x::facet_grid2(splits ~ .,
    switch = "y",
    scales = "free_y", space = "free_y",
    strip = ggh4x::strip_themed(
      background_y = ggh4x::elem_list_rect(
        fill = rev(c("grey", "blue", "pink"))
      ),
      text_y = ggh4x::elem_list_text(
        color = rev(c("red", "green", "yellow"))
      )
    )
  ) +
  scale_fill_brewer(palette = "Dark2") +
  theme(
    strip.placement = "outside",
    strip.text.y = element_text(
      size = 14, face = "bold", angle = 90,
      hjust = 0.5, vjust = 0.5
    ),
    legend.text = element_text(colour = "black", size = 18, face = "bold"),
    legend.title = element_text(size = 22, face = "bold"),
    plot.title = element_text(hjust = .5, size = 18, face = "bold"),
    axis.ticks = element_blank(), # size=0.25
    axis.text.y = ggtext::element_markdown(size = 18, face = "bold", hjust = 0.5),
    axis.title.y = element_text(
      margin = margin(r = 10), size = 28, face = "bold",
      color = "black"
    )
  )

enter image description here

Upvotes: 2

Related Questions