Javier Hernando
Javier Hernando

Reputation: 368

How to adjust the symbols overlayed over a bar plot?

I have created a divergent bar plot, and after the plot is created I want to add several symbols in order to indicate some information about then as a footnote. I provide the code to create the object p2 (the plot)

Afterwards I provide the function to add text. Specifically I have trouble how to display both symbols "+" and "*" in the space existent in the Olive oil chart. As you can see I would like to move them together.

The curiouous thing with these plots is if you scroll the Plot pane to the left or to the right in order to make the plot bigger (making smaller the console pane) these symbols move closer or further along the bars (2nd image is the same image but doing what I described)

Image1

Image2

The code adding the symbols



#CDKN2A

p2 <- p2 + geom_text(
    data = subset(df_plot2, gen == "CDKN2A" & grup_int == "1"),
    aes(label = "+"), 
    x = 1, y = "CDKN2A",
    vjust = 0, hjust = 8, size = 5
  ) 


#IFNG

p2 <-p2 + geom_text(
    data = subset(df_plot2, gen == "IFNG" & grup_int == "1"),
      aes(label = "+"), 
     x = 1, y = "IFNG",
     vjust = 0, hjust = 10, size = 5)
  

#NLRP3

p2 <-p2 + geom_text(
    data = subset(df_plot2, gen == "NLRP3" & grup_int == "1"),
      aes(label = "+"), 
     x = 1, y = "NLRP3",
     vjust = 0, hjust = -8, size = 5)



#PIK3CB

p2 <-p2 + geom_text(
    data = subset(df_plot2, gen == "PIK3CB" & grup_int == "1"),
      aes(label = c("+")), 
     x = 1, y = "PIK3CB",
     vjust = 0, hjust = -5, size = 5)

#TGFB2

p2 <-p2 + geom_text(
    data = subset(df_plot2, gen == "TGFB2" & grup_int == "1"),
      aes(label = "+"), 
     x = 1, y = "TGFB2",
     vjust = -1.2, hjust = -12, size = 5) +
  geom_text(
    data = subset(df_plot2, gen == "TGFB2" & grup_int == "1"),
      aes(label = "*"), 
     x = 1, y = "TGFB2",
     vjust = -1.2, hjust = -13, size = 5) +
  geom_text(
    data = subset(df_plot2, gen == "TGFB2" & grup_int == "2"),
      aes(label = "+"), 
     x = 1, y = "TGFB2",
     vjust = 0, hjust = -10, size = 5)


# NAMPT

p2 <-p2 + geom_text(
    data = subset(df_plot2, gen == "NAMPT" & grup_int == "3"),
      aes(label = "+"), 
     x = 1, y = "NAMPT",
     vjust = 0, hjust = 10, size = 5) + 
    geom_text(
    data = subset(df_plot2, gen == "NAMPT" & grup_int == "2"),
      aes(label = "*"), 
     x = 1, y = "NAMPT",
     vjust = 0, hjust = -5, size = 5)+ 
    geom_text(
    data = subset(df_plot2, gen == "NAMPT" & grup_int == "2"),
      aes(label = "#"), 
     x = 1, y = "NAMPT",
     vjust = 0, hjust = -6, size = 5)

#IL1B

p2 <-p2 + geom_text(
    data = subset(df_plot2, gen == "IL1B" & grup_int == "2"),
      aes(label = "#"), 
     x = 1, y = "IL1B",
     vjust = 0, hjust = -6, size = 5) 


The code to create the plot p2

df_plot2 <- structure(list(gen = structure(c(10L, 10L, 10L, 9L, 9L, 9L, 8L, 
8L, 8L, 7L, 7L, 7L, 6L, 6L, 6L, 5L, 5L, 5L, 4L, 4L, 4L, 3L, 3L, 
3L, 2L, 2L, 2L, 1L, 1L, 1L), levels = c("TGFB2", "PIK3CB", "NLRP3", 
"NFE2L2", "NAMPT", "IL1B", "IL10", "IFNG", "CDKN2A", "CD86"), class = "factor"), 
    grup_int = c(2, 3, 1, 3, 2, 1, 2, 1, 3, 2, 1, 3, 2, 3, 1, 
    2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 1, 3), time = c("3", 
    "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
    "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
    "3", "3", "3", "3", "3"), RQ_gapdh = c(1.018, 0.91, 0.949, 
    1.001, 1.065, 0.879, 0.947, 0.76, 0.924, 1.11, 1.014, 1.022, 
    1.123, 0.892, 1.007, 1.057, 0.809, 0.941, 1.106, 0.931, 0.983, 
    1.108, 0.983, 1.1, 1.13, 0.991, 1.112, 1.257, 1.37, 1.064
    ), Color = structure(c(2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 
    2L, 1L, 2L, 2L, 2L, 2L), levels = c("FALSE", "TRUE"), class = "factor")), row.names = c(9L, 
24L, 30L, 16L, 20L, 29L, 10L, 13L, 14L, 7L, 15L, 17L, 1L, 11L, 
23L, 2L, 19L, 25L, 6L, 18L, 26L, 5L, 21L, 27L, 4L, 22L, 28L, 
3L, 8L, 12L), class = "data.frame")


p1 <- ggplot(data = df_plot2, aes( x = RQ_gapdh, y = gen, xmin = after_scale(1), 
  fill = Color)) +
  geom_col(width = .5) +
    scale_fill_manual(values = c("lightseagreen", "orange2" ))+
  labs(
    y = "Genes", x = expression(2^{-Delta*Delta*Ct}),
    title = "Gene expression",
    subtitles = "1 year post-intervention") +   
  guides(fill = "none") +
  facet_wrap(~grup_int,
    labeller = labeller(
      grup_int = c(
        "1" = "Olive oil",
        "2" = "Nuts",
        "3" = "Low-fat diet"
      )
    )
  )  + 
  theme(axis.text.y = element_text(face = "italic", family = "serif"))+
   geom_vline(xintercept = 1, linetype = "dashed", color = "black")+
     scale_x_continuous(limits = c(0.75, 1.4), breaks = c(0.75, 1, 1.4))

# Editing font size, axis: customizing the  plot
# ggthemes economist https://github.com/javifar/ggthemes/blob/main/R/economist.R



p2 <- p1 + 
  theme_solarized(base_size = 14) +
  theme(legend.position = "right", 
        legend.title = element_blank()) +
  theme(axis.text.x = element_text(color = "gray0", size = 10, angle = 0, hjust = .5, vjust = .5, face = "plain"), axis.text.y = element_text(color = "gray0", size = 9, angle = 0, hjust = 1, vjust = 0, face = "plain"), axis.title.x = element_text(color = "gray0", size = 10, angle = 0, hjust = 0.5, vjust = 0.5, face = "plain"), axis.title.y = element_text(color = "gray0", size = 11, angle = 0, hjust = 1, vjust = 0.5, face = "plain"),plot.title=element_text(size=12, hjust=0.5, face="plain", colour="darkslateblue", vjust=2), plot.subtitle=element_text(size=10, hjust=0.5, face="plain", colour="darkslateblue", vjust=2),
axis.ticks.x = element_line(linetype = "dashed"))




p2 <- p1 + 
  theme_solarized(base_size = 14) +
  theme(legend.position = "right", 
        legend.title = element_blank()) +
  theme(axis.text.x = element_text(color = "gray0", size = 9, angle = 0, hjust = .5, vjust = .5, face = "plain"), axis.text.y = element_text(color = "gray0", size = 11, angle = 0, hjust = 1, vjust = 0, face = "plain"), axis.title.x = element_text(color = "gray0", size = 10, angle = 0, hjust = 0.5, vjust = 0.5, face = "plain"), axis.title.y = element_text(color = "gray0", size = 11, angle = 0, hjust = 1, vjust = 0.5, face = "plain"),plot.title=element_text(size=12, hjust=0.5, face="plain", colour="darkslateblue", vjust=2), plot.subtitle=element_text(size=10, hjust=0.5, face="plain", colour="darkslateblue", vjust=2),
axis.ticks.x = element_line(linetype = "dashed")) +
  theme(axis.text.y = element_text(face = "italic", family = "serif"))


Upvotes: 0

Views: 59

Answers (1)

stefan
stefan

Reputation: 125418

First, instead of using a geom_text for each of your labels add a new column to you data with the symbol you want to for each row or an empty string if no symbol is required. And if you want multiple symbols then add them as one label. Afterwards you could use one geom_text to add your labels:

library(ggplot2)
library(ggthemes)

df_plot2 <- df_plot2 |>
  dplyr::mutate(
    label = dplyr::case_when(
      gen %in% c("CDKN2A", "IFNG", "NLRP3", "PIK3CB") & grup_int == "1" ~ "+",
      gen %in% c("TGFB2") & grup_int == "1" ~ "+*",
      gen %in% c("TGFB2") & grup_int == "2" ~ "+",
      gen %in% c("NAMPT") & grup_int == "3" ~ "+",
      gen %in% c("NAMPT") & grup_int == "2" ~ "*#",
      gen %in% c("IL1B") & grup_int == "2" ~ "*#",
      .default = ""
    ),
    hjust = dplyr::if_else(RQ_gapdh > 1, 0, 1)
  )

ggplot(data = df_plot2, aes(
  x = RQ_gapdh, y = gen, xmin = after_scale(1),
  fill = Color
)) +
  geom_col(width = .5) +
  geom_label(aes(label = label, hjust = hjust), size = 5, label.size = 0, fill = NA) +
  scale_fill_manual(values = c("lightseagreen", "orange2")) +
  labs(
    y = "Genes", x = expression(2^{
      -Delta * Delta * Ct
    }),
    title = "Gene expression",
    subtitles = "1 year post-intervention"
  ) +
  guides(fill = "none") +
  facet_wrap(~grup_int,
    labeller = labeller(
      grup_int = c(
        "1" = "Olive oil",
        "2" = "Nuts",
        "3" = "Low-fat diet"
      )
    )
  ) +
  theme_solarized(base_size = 14) +
  theme(axis.text.y = element_text(face = "italic", family = "serif")) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "black") +
  scale_x_continuous(limits = c(0.75, 1.4), breaks = c(0.75, 1, 1.4))

Upvotes: 2

Related Questions