Overlay two ggplots
1
0
Entering edit mode
4.9 years ago

I am making bar plots using ggplot for the GO enrichment analysis I did. Now I have upregulated Go terms and downregulated GO terms. My data looks like this:

head(Up)

                   GO         Percentage     q.val
1       extracellular region   4.959008 2.154712e-10
2  extracellular region part   2.899420 6.910822e-09
3        extracellular space   2.279544 6.910822e-09
4            immune response   4.109178 4.010631e-08
5       response to wounding   3.779244 5.724871e-08

head(Down)

                   GO         Percentage q.val
1       extracellular region   0.000000    NA
2  extracellular region part   0.000000    NA
3        extracellular space   0.000000    NA
4            immune response   0.019996    NA
5       response to wounding   6.596415    3.109178e-05

Currently I am making two plot

U <- ggplot(data = Up, aes(x = GO, y = Percentage)) +
  geom_bar(stat = "identity", aes(fill =-10*log10(q.val)) +
  labs(fill = "Positive log10 FDR")+
  theme(plot.margin = margin(6,.5,1,.5, "cm"), axis.text.x = element_text(
    angle = 90,
    hjust = 1,
    vjust = 0.5,
    size=13
  ), axis.text.y  = element_text(
    size=12
  ))

D <- ggplot(data = Down, aes(x = GO, y = Percentage)) +
  geom_bar(stat = "identity", aes(fill =-10*log10(q.val))) +
  labs(fill = "Positive log10 FDR")+
  theme(plot.margin = margin(6,.5,1,.5, "cm"), axis.text.x = element_text(
    angle = 90,
    hjust = 1,
    vjust = 0.5,
    size=13
  ), axis.text.y  = element_text(
    size=12
  ))

After generating the plots in R, i am using inkscape to overlay the two plots into one. Is there an efficient way in R to do so?

R • 7.0k views
ADD COMMENT
0
Entering edit mode

This brings both the plot in one picture, but I want the Go terms to appear once and upregulated bar plots at one side and downregulated bar plot on the other side. This means the GO terms would be once in the middle.

ADD REPLY
0
Entering edit mode

Combine the data frames? Your plotting logic is identical so combining the datasets being plotted should plot them appropriately.

Also, please show us what your plots look like right now and how you'd like them to look. See this post on how to upload images.

ADD REPLY
0
Entering edit mode
4.9 years ago
AK ★ 2.2k

Hi saamar.rajput,

Not sure which one you meant:

barplot_updown

Up$Regulation <- rep("Up", nrow(Up))
Down$Regulation <- rep("Down", nrow(Down))
df <- rbind(Up, Down)
df$Percentage <-
  ifelse(df$Regulation == "Down", -df$Percentage, df$Percentage)
ggplot(df, aes(GO, Percentage, fill = -10 * log10(q.val))) +
  geom_bar(stat = "identity") +
  labs(fill = "Positive log10 FDR") +
  scale_y_continuous(
    expand = c(0, 0),
    breaks = pretty(df$Percentage),
    labels = abs(pretty(df$Percentage))
  ) +
  theme_bw() +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_text(angle = 60, hjust = 1))

Or: barplot_sides

Up$Regulation <- rep("Up", nrow(Up))
Down$Regulation <- rep("Down", nrow(Down))
df <- rbind(Up, Down)
ggplot(df, aes(Regulation, Percentage, fill = -10 * log10(q.val))) +
  geom_bar(stat = "identity",
           position = "dodge2") +
  labs(fill = "Positive log10 FDR") +
  scale_y_continuous(expand = c(0, 0)) +
  theme_bw() +
  theme(axis.title.x = element_blank()) +
  facet_grid(cols = vars(GO), switch = "both")
ADD COMMENT

Login before adding your answer.

Traffic: 2601 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6