Question: Overlay two ggplots
0
gravatar for saamar.rajput
17 months ago by
Germany
saamar.rajput60 wrote:

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 • 2.0k views
ADD COMMENTlink modified 17 months ago by AK1.9k • written 17 months ago by saamar.rajput60

https://stackoverflow.com/questions/40297206/overlaying-two-plots-using-ggplot2-in-r?rq=1

ADD REPLYlink written 17 months ago by Alex Reynolds31k

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 REPLYlink written 17 months ago by saamar.rajput60

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 REPLYlink modified 17 months ago • written 17 months ago by RamRS30k
0
gravatar for AK
17 months ago by
AK1.9k
AK1.9k wrote:

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 COMMENTlink modified 17 months ago • written 17 months ago by AK1.9k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1391 users visited in the last hour