简体   繁体   中英

Change order y-axis of dotplot in ggplot2

I have data as below with which I want to make a dotplot. Ggplot automatically orders the Y-axis alphabetically. I want the dotplot to be configured that it the y-axis is ordered based on the values of the X-axis, from small to large. Please see below for example code, a figure of the current outcome and a figure of example desired outcome. Thanks!

Example code:

# import package
library("ggplot2")
# create fake data
set.seed(1024) # keep reproducibility
go <- paste0("GO", sample(1000:2000, 5))
Count <- sample(1:20, 10)
data <- data.frame("GOs" = rep(go, 2), 
                   "Condition" = rep(c("A", "B"), each = 5),
                   "GeneRatio" = 1 / sample(10, 10), 
                   "p.adjust" = 0.05 / sample(10, 10),
                   "Count" = Count)

# plot: dot plot
ggplot(data = data, aes(x = GeneRatio, y = GOs, 
                        color = `p.adjust`, size = Count)) + 
  geom_point() +
  scale_color_gradient(low = "red", high = "blue") +
  theme_bw() + 
  ylab("") + 
  xlab("") + 
  ggtitle("GO enrichment analysis")

Current outcome: 在此处输入图像描述

Desired outcome should be something like this:

在此处输入图像描述

data %>% 
  mutate(GOs = fct_reorder2(GOs, GeneRatio, -`p.adjust`)) ->
  data

ggplot(data = data, 
       aes(x = GeneRatio, y = GOs, 
           color = `p.adjust`, 
           size = Count)) + 
  geom_point() +
  scale_color_gradient(low = "red", high = "blue") +
  theme_bw() + 
  ylab("") +
  xlab("") +
  ggtitle("GO enrichment analysis")

以上产生了这个情节

In order to achieve the output desired you would need to have one x value per y value. One solution is to concatenate GOs and Condition to plot "GO1308_A" and "GO1308_B" separately.

data$GOsCondi <- paste0(data$GOs,"_",data$Condition)
ggplot(data = data, aes(x = GeneRatio, y = reorder(GOsCondi,GeneRatio), 
                        color = `p.adjust`, size = Count)) + 
  geom_point() +
  scale_color_gradient(low = "red", high = "blue") +
  theme_bw() + 
  ylab("") + 
  xlab("") + 
  ggtitle("GO enrichment analysis")

在此处输入图像描述

I can think of two solutions that will solve your issue. For clarity the plots are faceted by condition.

Option 1 (modified version of previous answer):

library(tidyverse)

data_ordered  <- data %>%
  group_by(Condition) %>% # group by condition
  mutate(GOs = fct_reorder2(GOs, GeneRatio,-GeneRatio)) %>%  # reorder GOs by GeneRatio
  ungroup()

# plot 1
ggplot(data = data_ordered,
       aes(
         x = GeneRatio,
         y = GOs,
         color = `p.adjust`,
         size = Count
       )) +
  geom_point() +
  scale_color_gradient(low = "red", high = "blue") +
  theme_bw() +
  ylab("Ontology") +
  xlab("GeneRatio") +
  ggtitle("GO enrichment analysis") +
  facet_grid( ~ Condition)# facet plot by condition

选项1

Option 2:

data_ordered_2 <- data %>% 
  group_by(Condition) %>% # group by condition
  arrange(Condition, GeneRatio) %>% # arrange by Condition and GeneRatio
  mutate(GOs_ordered = factor (row_number(), labels = GOs)) # add column containing ordered GOs

# plot 2
ggplot(data = data_ordered_2,
       aes(
         x = GeneRatio,
         y = GOs_ordered,
         color = `p.adjust`,
         size = Count
       )) +
  geom_point() +
  scale_color_gradient(low = "red", high = "blue") +
  theme_bw() +
  ylab("Ontology") +
  xlab("GeneRatio") +
  ggtitle("Option 2") +
  facet_grid( ~ Condition)# facet plot by condition

选项 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM