如何在R中分开发散的条形图 [英] How to part diverging bar plots in R

查看:137
本文介绍了如何在R中分开发散的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在R/ggplot2中相对较新,我想就如何创建如下图寻求一些建议:

Hi I am relatively new in R / ggplot2 and I would like to ask for some advice on how to create a plot that looks like this:

说明:一个分散的条形图,显示了生物学功能,其表达增加的基因(黄色)指向右侧,而表达降低的基因(紫色)指向左侧.条的长度代表差异表达基因的数目,并且颜色强度根据它们的p值而变化.

Explanation: A diverging bar plot showing biological functions with genes that have increased expression (yellow) pointing towards the right, as well as genes with reduced expression (purple) pointing towards the left. The length of the bars represent the number of differentially expressed genes, and color intensity vary according to their p-values.

请注意,x轴在两个方向上都必须为正". (在已发表的有关基因表达实验研究的文献中,指向左侧的条表示表达减少的基因,而右侧的条表示表达增加的基因.该图的目的不是显示变化的幅度"(即会产生正值和负值).相反,我们试图绘制具有表达变化的基因数目,因此不能为负数

Note that the x-axis must be 'positive' in both directions. (In published literature on gene expression experimental studies, bars that point towards the left represent genes that have reduced expression, and right to show genes that have increased expression. The purpose of the graph is not to show the "magnitude" of change (which would give rise to positive and negative values). Instead, we are trying to plot the NUMBER of genes that have changes of expression, therefore cannot be negative)

我尝试了ggplot2,但是完全无法重现显示的图形. 这是我要绘制的数据:单击此处获取链接

I have tried ggplot2 but fails completely to reproduce the graph that is shown. Here is the data which I am trying to plot: Click here for link

> dput(sample)
structure(list(Name = structure(c(15L, 19L, 5L, 11L, 8L, 6L, 
16L, 13L, 17L, 1L, 3L, 2L, 14L, 18L, 7L, 12L, 10L, 9L, 4L, 20L
), .Label = c("Actin synthesis", "Adaptive immunity", "Antigen presentation", 
"Autophagy", "Cell cycle", "Cell division", "Cell polarity", 
"DNA repair", "Eye development", "Lipid metabolism", "Phosphorylation", 
"Protein metabolism", "Protein translation", "Proteolysis", "Replication", 
"Signaling", "Sumoylation", "Trafficking", "Transcription", "Translational initiation"
), class = "factor"), Trend_in_AE = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Down", "Up"), class = "factor"), Count = c(171L, 
201L, 38L, 63L, 63L, 47L, 22L, 33L, 20L, 16L, 16L, 7L, 10L, 4L, 
13L, 15L, 5L, 7L, 9L, 7L), PValue = c(1.38e-08, 1.22e-06, 1.79e-06, 
2.89e-06, 0.000122, 0.000123, 0.00036, 0.000682, 0.001030253, 
0.001623939, 7.76e-05, 0.000149, 0.000734, 0.001307039, 0.00292414, 
0.003347556, 0.00360096, 0.004006781, 0.007330264, 0.010083734
)), .Names = c("Name", "Trend_in_AE", "Count", "PValue"), class = "data.frame", row.names = c(NA, 
-20L))

非常感谢您的帮助和建议,这对我的学习非常有帮助.

Thank you very much for your help and suggestions, this is really help with my learning.

我自己的谦虚尝试是这样的:

My own humble attempt was this:

table <- read.delim("file.txt", header = T, sep = "\t")
library(ggplot2)
ggplot(aes(x=Number, y=Names)) + 
  geom_bar(stat="identity",position="identity") + 
  xlab("number of genes") + 
  ylab("Name"))

结果是关于aes的错误消息

Result was error message regarding the aes

推荐答案

虽然并不是您要找的东西,但是以下内容可以帮助您入门.正如表达所说,@热那亚没有免费的午餐".因此,按照这种精神,就像@dww正确指出的那样,请显示一定的努力"!

Although not exactly what you are looking for, but the following should get you started. @Genoa, as the expression goes, "there are no free lunches". So in this spirit, like @dww has rightly pointed out, show "some effort"!

# create dummy data
df <- data.frame(x = letters,y = runif(26))
# compute normalized occurence for letter
df$normalize_occurence <- round((df$y - mean(df$y))/sd(df$y), 2)  
# categorise the occurence
df$category<- ifelse(df$normalize_occurence >0, "high","low")
# check summary statistic
summary(df)
       x            y           normalize_occurence 
a      : 1   Min.   :0.00394   Min.   :-1.8000000  
b      : 1   1st Qu.:0.31010   1st Qu.:-0.6900000  
c      : 1   Median :0.47881   Median :-0.0800000  
d      : 1   Mean   :0.50126   Mean   : 0.0007692  
e      : 1   3rd Qu.:0.70286   3rd Qu.: 0.7325000  
f      : 1   Max.   :0.93091   Max.   : 1.5600000  
(Other):20                                         
category        
Length:26         
Class :character  
Mode  :character 

ggplot(df,aes(x = x,y = normalize_occurence)) + 
      geom_bar(aes(fill = category),stat = "identity") +
      labs(title= "Diverging Bars")+
      coord_flip()

这篇关于如何在R中分开发散的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆