Adding different labels to different facets of the same plot

Hi there-

I am interested in making a figure that has labels denoting statistical significance that are different for each group that I have plotted in separate facets. I have no idea how to do this so I would appreciate any help! Here is the code that I am working with:

sp_NR_qPCR <- ggplot(qPCR_data, mapping=aes(x=Sample, y=delCt, color=Gene))+ 
  geom_point(show.legend=FALSE)+
  geom_smooth(aes(fill=Gene), show.legend=FALSE)+
  facet_grid(.~Gene)+
  scale_x_continuous(breaks=c(-25, -17, -1, 7))+
  labs(y="Relative abundance (∆Ct)", x="Hours relative to peak secretion (0 h)")+
  theme_classic()+
  theme(axis.title.x=element_text(size=10, face="bold"), axis.title.y=element_text(size=10, face="bold"))+
  theme(axis.text.x=element_text(size=10), axis.text.y=element_text(size=10))+
  theme(strip.text.x=element_text(size=12, face="italic"))+
  scale_color_brewer(palette="Set1")+
  scale_fill_brewer(palette="Set1")+
  theme(panel.border=element_rect(fill = NA, color="black"))

sp_NR_qPCR

and it produces a figure that looks like this:

BUT I would like the figure to look like this, with the respective labels above the point distributions, and the labels on top of each individual facet as well:

Figure1.pdf (215.8 KB)

Sorry that they are different formats. Let me know if you need me to re-upload in a better format.

Thanks,

Erik

Could you dput(qPCR_data) for us so that we could try to run your code ?

sorry, I'm not sure how to do dput()

you put the name of your object in it, as I wrote out for you.
run it in the console.
it prints out a result
you share that result here.
you can google dput and read about it of course.
or ?dput on the console for the inbuilt help.

> dput(qPCR_data)
structure(list(Sample = c(-25, -25, -25, -17, -17, -17, -1, -1, 
-1, 7, 7, 7, -25, -25, -25, -17, -17, -17, -1, -1, -1, 7, 7, 
7, -25, -25, -25, -17, -17, -17, -1, -1, -1, 7, 7, 7), Gene = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("CpNRT1.5", "CpNR", "CpNiR"), class = "factor"), 
    delCt = c(-5.921332359, -6.145671844, -5.677146912, -2.748710632, 
    -3.19631958, -2.737405777, 1.386582375, 1.850103378, 0.789480209, 
    -6.969926834, -3.663425446, -5.600616455, -3.526859283, -4.287014008, 
    -5.624802589, 0.251309395, -0.86595726, -1.414552689, 3.509215355, 
    3.678625107, 2.386909485, -2.047849655, -0.381763458, -2.709918022, 
    -2.374889374, -2.108045578, -2.310191154, -2.656218529, -2.271496773, 
    -1.574661255, 2.764549255, 2.920114517, 2.368210793, -5.035234451, 
    -2.270872116, -3.361042023)), row.names = c(NA, -36L), spec = structure(list(
    cols = list(Sample = structure(list(), class = c("collector_double", 
    "collector")), Gene = structure(list(), class = c("collector_character", 
    "collector")), delCt = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Thanks!

Hi @nigrahamuk, Sorry I forgot to mention that I re-ordered the levels of Gene using the code:

qPCR_data$Gene <- factor(qPCR_data$Gene, levels=c("CpNRT1.5", "CpNR", "CpNiR"))
levels(qPCR_data$Gene)

I think you need to identify a principled way to set text for the points you want, I chose the first 4 letters, and the highest value per gene per sample to show it for (others Na).

library(tidyverse)

structure(list(
  Sample = c(
    -25, -25, -25, -17, -17, -17, -1, -1,
    -1, 7, 7, 7, -25, -25, -25, -17, -17, -17, -1, -1, -1, 7, 7,
    7, -25, -25, -25, -17, -17, -17, -1, -1, -1, 7, 7, 7
  ), Gene = structure(c(
    1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
    3L, 3L, 3L
  ), .Label = c("CpNRT1.5", "CpNR", "CpNiR"), class = "factor"),
  delCt = c(
    -5.921332359, -6.145671844, -5.677146912, -2.748710632,
    -3.19631958, -2.737405777, 1.386582375, 1.850103378, 0.789480209,
    -6.969926834, -3.663425446, -5.600616455, -3.526859283, -4.287014008,
    -5.624802589, 0.251309395, -0.86595726, -1.414552689, 3.509215355,
    3.678625107, 2.386909485, -2.047849655, -0.381763458, -2.709918022,
    -2.374889374, -2.108045578, -2.310191154, -2.656218529, -2.271496773,
    -1.574661255, 2.764549255, 2.920114517, 2.368210793, -5.035234451,
    -2.270872116, -3.361042023
  )
), row.names = c(NA, -36L), spec = structure(list(
  cols = list(Sample = structure(list(), class = c(
    "collector_double",
    "collector"
  )), Gene = structure(list(), class = c(
    "collector_character",
    "collector"
  )), delCt = structure(list(), class = c(
    "collector_double",
    "collector"
  ))), default = structure(list(), class = c(
    "collector_guess",
    "collector"
  )), skip = 1
), class = "col_spec"), class = c(
  "spec_tbl_df",
  "tbl_df", "tbl", "data.frame"
)) -> qPCR_data


#arbitrary label text for first sample of every gene
qPCR_data2 <- qPCR_data %>% group_by(Gene) %>% mutate(label_text=letters[dense_rank(Sample)]) 
qPCR_data3 <- arrange(qPCR_data2,Gene,Sample,desc(delCt)) %>% group_by(Gene,label_text) %>% slice(1)

qPCR_data4 <- left_join(qPCR_data,
                        qPCR_data3)
sp_NR_qPCR <- ggplot(qPCR_data4, mapping=aes(x=Sample, y=delCt, color=Gene))+ 
  geom_point(show.legend=FALSE)+
  geom_smooth(aes(fill=Gene), show.legend=FALSE)+
  geom_text(aes(label=label_text), color="Black",nudge_y = 1.1) +
  facet_grid(.~Gene)+
  scale_x_continuous(breaks=c(-25, -17, -1, 7))+
  labs(y="Relative abundance (∆Ct)", x="Hours relative to peak secretion (0 h)")+
  theme_classic()+
  theme(axis.title.x=element_text(size=10, face="bold"), axis.title.y=element_text(size=10, face="bold"))+
  theme(axis.text.x=element_text(size=10), axis.text.y=element_text(size=10))+
  theme(strip.text.x=element_text(size=12, face="italic"))+
  scale_color_brewer(palette="Set1")+
  scale_fill_brewer(palette="Set1")+
  theme(panel.border=element_rect(fill = NA, color="black"))

sp_NR_qPCR

OK thanks, it looks good so far. Would you mind just giving me a brief explanation of each command that was used to generate qPCR_data2, qPCR_data3, qPCR_data4?

Also is there a way to make the labels different for each facet? If you can see from my "ideal" figure, they are not quite the same. For example, NRT1.5 goes "abca" whereas NR goes "abcb".

Thanks,

Erik

you can get help on each function used by the question mark in the console
i.e.
?slice

slice {dplyr}	R Documentation
Choose rows by position
Description
Choose rows by their ordinal position in the tbl. Grouped tbls use the ordinal position within the group.

added an extra step to make every label unique so you can see its up to you what the values are and accross facets, because its an explicit method.

qPCR_data3 <- arrange(qPCR_data2,Gene,Sample,desc(delCt)) %>% group_by(Gene,label_text) %>% slice(1) 
qPCR_data3$label_text <- LETTERS[1:12]
> qPCR_data3 
# A tibble: 12 x 4
# Groups:   Gene, label_text [12]
   Sample Gene      delCt label_text
    <dbl> <fct>     <dbl> <chr>     
 1    -25 CpNRT1.5 -5.68  A         
 2    -17 CpNRT1.5 -2.74  B         
 3     -1 CpNRT1.5  1.85  C         
 4      7 CpNRT1.5 -3.66  D         
 5    -25 CpNR     -3.53  E         
 6    -17 CpNR      0.251 F         
 7     -1 CpNR      3.68  G         
 8      7 CpNR     -0.382 H         
 9    -25 CpNiR    -2.11  I         
10    -17 CpNiR    -1.57  J         
11     -1 CpNiR     2.92  K         
12      7 CpNiR    -2.27  L  
qPCR_data4 <- left_join(qPCR_data, qPCR_data3)

simpler

qPCR_data3$label_text <- c("a","b","c","b"...... etc

i am facing some difficulty the paramter list . can someone help ?

You should be as specific as you can about what help you need

OK this works perfectly. THANKS!!! One final request, though. How do I put "(a)" "(b)" and "(c)" on the top left corner outside of each facet, from left to right as in the original .pdf I shared?

by far the easiest way to get close is just to change your Gene names

CpNRT1.5 to `a) CpNRT1.5`

etc.

Yeah that makes sense, however I would prefer to have the face for the letter NOT be italic, but the gene name be italic. Also ideally the letter for the figure would be positioned above the facets, but this isn't essential.

I've never tried to customise a chart to this extent so I'll bow out in favour of other ggplot experts

Hi @nirgrahamuk thanks for all of your help with adding labels above the data.

I was actually able to figure out a (somewhat crude) way to add the labels on top of the panels in the figure, I just made each figure individually, using the labs(tag="tag") function to put the label tag on the top right corner.

I then used ggarrange() to arrange all of the plots. I think ggarrange() is a part of the ggpubr package, which will need to be installed.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.