Alex
Alex

Reputation: 371

Facet_wrap labels as panel labels in ggplot

I'm working on a gene expression profile plot where I've faceted the profiles by cluster number. I've been able to get the plot pretty close to what I'm looking for, but I would like to be able to make this with the panel labels in the upper right-hand corner of each respective plotting area.

Here are my data:

structure(list(time_point = c("10", "10", "10", "10", "10", "10", 
"10", "10", "10", "13", "13", "13", "13", "13", "13", "13", "13", 
"13", "24", "24", "24", "24", "24", "24", "24", "24", "24", "35", 
"35", "35", "35", "35", "35", "35", "35", "35"), cluster = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", 
"6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
"1", "2", "3", "4", "5", "6", "7", "8", "9"), mean_value = c(-0.426083520451465, 
0.604608926627412, 0.76451364891251, -0.471139779941961, 0.2351694555588, 
-0.705679926146899, -0.454858199321039, -0.165115160845773, 0.724407409818679, 
0.781438905910975, 0.146965540891108, -0.769313576080577, -0.364131220020667, 
-0.977858147148868, 0.723255456138281, 1.27112335113834, -0.32602591779613, 
-0.411066723030554, -0.229664052498654, -0.854136058469294, -0.66018500031545, 
1.2563451985156, 0.544812333405859, 0.693110024751582, -0.350948607542015, 
-0.424710073864252, -0.160578456515796, -0.125691332960858, 0.102561590950772, 
0.664984927483517, -0.421074198552966, 0.197876358184206, -0.710685554742966, 
-0.465316544275294, 0.915851152506157, -0.15276223027233), sd = c(0.0492840366570553, 
0.51236309986752, 0.0969492719939365, 0.0586339372406152, 0.0929657760623659, 
0.0473263114084479, 0.0276800874550852, 0.0735227878362672, 1.0416084268533, 
1.1513324603848, 0.193252849305954, 0.0172156507400275, 0.0327622261605831, 
0.0538099322569242, 0.109168493202137, 0.481243962369782, 0.0826879496732078, 
0.0388480633488431, 0.159249818818607, 0.0417074333259725, 0.042500837893243, 
0.0527404368558342, 0.0688903901671137, 0.0593712415929212, 0.0152356442432683, 
0.0577220450239637, 0.0930169866292114, 0.204810043584311, 0.177841591444567, 
0.236871574361693, 0.0369824877579054, 0.210415560702688, 0.067153094681514, 
0.043764627685752, 0.429144691634867, 0.0203773261748577)), row.names = c(NA, 
-36L), groups = structure(list(time_point = c("10", "13", "24", 
"35"), .rows = structure(list(1:9, 10:18, 19:27, 28:36), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 4L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

And my current ggplot code:

# Adjustments to ggplot theme
Alex_Theme = theme_bw() +
  theme(plot.title = element_text(hjust = 0.5, face='plain', size = 12)) +
  theme(plot.title = element_text(vjust=0)) +
  theme(plot.subtitle=element_text(size=10, hjust=0.5, face="italic", color="black")) +
  #theme(legend.position= "none") +
  theme(panel.border = element_rect(fill=NA, colour = "black", size=0.5)) +
  theme(axis.text = element_text(face = "plain", size = 12)) +
  theme(axis.title.x = element_text(margin = margin(t = 6, r = 20, b = 0, l = 0))) +
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 6, b = 0, l = 0))) +
  #theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(axis.title = element_text(face="plain", size = 12))

# colorblind pallet :)
cbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "red")

# named vector of labels for facet
facet_labels <- c("A.", "B.", "C.", "D.", "E.", "F.", "G.", "H.", "I.")
names(facet_labels) <- c("1", "2", "3", "4", "5", "6", "7", "8", "9")

# plotting
ggplot(centroids_long_summary, aes(x=time_point,y=mean_value, group=cluster, colour=as.factor(cluster))) + 
  Alex_Theme +
  # Facet_Theme +
  geom_line() +
  geom_point(size = 1) +
  geom_errorbar(aes(ymin= mean_value - sd, ymax= mean_value + sd), width=0.1) +
  ylab("Gene expression") +
  xlab("Developmental stage") +
  scale_x_discrete(labels = c("Pre", "Dia-C", "Quies", "Post")) +
  scale_color_manual(name = "Cluster", values = cbPalette) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position= "none") +
  facet_wrap(.~cluster, ncol = 3, labeller = labeller(cluster = facet_labels)
             ) +
  theme(strip.background = element_rect(fill = "white", color = "white"),
        strip.text.x = element_text(hjust = 0, size = 12)) 

Produces something like this: enter image description here

However, I'd like to try and get the panel labels inside the plotting areas (preferably without the space that a blank facet label would leave between each row).

Upvotes: 1

Views: 1747

Answers (1)

Jon Spring
Jon Spring

Reputation: 66480

I added a geom_text layer that just sees one row for each cluster (otherwise the text will be overplotted for every row in the data), and hid the facet names in theme(strip.text.x = ...).

ggplot(centroids_long_summary, aes(x=time_point,y=mean_value, group=cluster, colour=as.factor(cluster))) + 
  Alex_Theme +
  # Facet_Theme +
  geom_line() +
  geom_point(size = 1) +
  geom_errorbar(aes(ymin= mean_value - sd, ymax= mean_value + sd), width=0.1) +
  geom_text(data = centroids_long_summary %>% 
              group_by(cluster) %>%
              slice(1) %>%
              mutate(label = paste0(LETTERS[as.numeric(cluster)],".")),
            aes(label = label, x = 0.8, y = 1.8), color = "black", fontface = "bold") +
  ylab("Gene expression") +
  xlab("Developmental stage") +
  scale_x_discrete(labels = c("Pre", "Dia-C", "Quies", "Post")) +
  scale_color_manual(name = "Cluster", values = cbPalette) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position= "none") +
  facet_wrap(.~cluster, ncol = 3) +
  theme(strip.text.x = element_blank(),
        strip.background = element_rect(fill = "white", color = "white"))

enter image description here

Upvotes: 3

Related Questions