R語言柱狀圖排序和x軸上的標(biāo)簽傾斜操作
R語言做柱狀圖大致有兩種方法, 一種是基礎(chǔ)庫里面的 barplot函數(shù), 另一個(gè)就是ggplot2包里面的geom_bar
此處用的是字符變量 統(tǒng)計(jì)其各頻數(shù),然后做出其柱狀圖。(橫軸上的標(biāo)簽顯示不全)
t <- sort(table(dat1$L), decreasing = TRUE) #將頻數(shù)表進(jìn)行排序 r <- barplot(t, col = "blue", main = "柱狀圖", ylim = c(0,12), names.arg = dimnames(t) #畫字符變量的柱狀圖 tmp <- as.vector(t) #將頻數(shù)變成一個(gè)向量 text(r, tmp, label = tmp, pos = 3) #加柱子上面的標(biāo)簽
或用ggplot2包 (目前仍沒有給柱子上加數(shù)字標(biāo)簽)
library(ggplot2) #加載ggplot2包 reorder_size <- function(x) { factor(x, levels = names(sort(table(x)))) } #自定義函數(shù),獲取因子型變量的因子類型 p <- ggplot(dat3, aes(reorder_size(LAI))) + #用因子變量做基礎(chǔ)底圖,也可直接用reorder排序 geom_bar(fill = "blue") + #畫柱狀圖 theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5)) + #讓橫軸上的標(biāo)簽傾斜45度 xlab("柱狀圖") #給x軸加標(biāo)簽
補(bǔ)充:R 語言條形圖,解決x軸文字排序問題
數(shù)據(jù)結(jié)果的圖形展示,R代碼,《R數(shù)據(jù)科學(xué)》是個(gè)好東西
數(shù)據(jù)格式如下:
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) #轉(zhuǎn)換 library(tidyverse) godata<-as_tibble(godata) class(godata) #原始數(shù)據(jù)篩選(category,term,pval)散列,按照category,-log10(pval)排序 data<-godata%>%select(category,term,pval)%>%arrange(category,desc(-log10(pval))) #畫圖時(shí)改變geom_bar的自動(dòng)排序 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()
結(jié)果:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
R語言實(shí)現(xiàn)支持向量機(jī)SVM應(yīng)用案例
本文主要介紹了R語言實(shí)現(xiàn)支持向量機(jī)SVM應(yīng)用案例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08R語言學(xué)習(xí)初識(shí)Rcpp類型List
這篇文章主要為大家介紹了R語言中Rcpp的類型List的基礎(chǔ)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11Rstudio中安裝package出現(xiàn)的問題及解決
這篇文章主要介紹了Rstudio中安裝package出現(xiàn)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04R語言-使用ifelse進(jìn)行數(shù)據(jù)分組
這篇文章主要介紹了R語言-使用ifelse進(jìn)行數(shù)據(jù)分組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04