简体   繁体   English

在ggplot2中更改点图的y轴顺序

[英]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. Ggplot 自动按字母顺序排列 Y 轴。 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.我希望将点图配置为根据 X 轴的值从小到大对 y 轴进行排序。 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.为了获得所需的输出,您需要每个 y 值有一个 x 值。 One solution is to concatenate GOs and Condition to plot "GO1308_A" and "GO1308_B" separately.一种解决方案是连接 GO 和 Condition 以分别绘制“GO1308_A”和“GO1308_B”。

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):选项1(先前答案的修改版本):

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:选项 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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