R 语言条形图,解决x轴文字排序问题

数据结果的图形展示,R代码,《R数据科学》是个好东西

数据格式如下:

term category pval
neutrophil chemotaxis biological_process 1.68E-09
innate immune response biological_process 3.35E-09
complement activation, classical pathway biological_process 1.14E-08
negative regulation of endopeptidase activity biological_process 4.43E-08
collagen fibril organization biological_process 4.43E-08
blood coagulation biological_process 1.29E-07
proteolysis involved in cellular protein catabolic process biological_process 1.56E-07
proteolysis biological_process 1.13E-06
leukocyte migration involved in inflammatory response biological_process 1.47E-06
peptide cross-linking biological_process 1.47E-06
extracellular space cellular_component 8.75E-40
collagen-containing extracellular matrix cellular_component 2.08E-26
extracellular matrix cellular_component 5.72E-11
lysosome cellular_component 6.09E-10
extracellular region cellular_component 6.58E-10
collagen trimer cellular_component 1.68E-09
cell surface cellular_component 2.80E-08
extracellular exosome cellular_component 2.34E-07
extrinsic component of external side of plasma membrane cellular_component 1.47E-06
sarcolemma cellular_component 3.16E-06

 

作图要求:x轴为term,颜色按categroy分类、并且pval由小到大排序

代码:

#openxlsx读入为data.frame

class(data)

#转换
library(tidyverse)
godata<-as_tibble(godata)
class(godata)

#原始数据筛选(category,term,pval)散列,按照category,-log10(pval)排序

data<-godata%>%select(category,term,pval)%>%arrange(category,desc(-log10(pval)))

#画图时改变geom_bar的自动排序

data$term<-factor(data$term,levels = unique(data$term),ordered = T)

#作图

ggplot(data)+
  geom_bar(aes(x=term,y=-log10(pval),fill=category),stat = 'identity')+
  coord_flip()

结果:

R 语言条形图,解决x轴文字排序问题