R语言:ggplot2做柱状图,随机生成颜色。

#加载包

> library(ggplot2)

> library(tidyverse)

> library(openxlsx)

> library(reshape2)

> library(RColorBrewer)

> library(randomcoloR)

> library(viridis)

> set.seed(1233) #设立种子数。

> palette <- distinctColorPalette(30) #生成随机30个颜色。

> write.table(palette, file = "30genus color.txt", sep = ",") #记录随机生成的颜色

> setwd("设立目录")

> mydata <- read.xlsx("读取.xlsx")

> data_draw<-melt(mydata,id.vars='species')#转换数据

复制代码
> head(mydata)
                   species      T1     T2     T3     T4      T5     T6     T7     T8     T9
1 Streptococcus salivarius   44092 121843 311183 486756 1156014 223453 163548  31383  47748
2      Rothia mucilaginosa  389451 347085 151944 159476  249279  92135 405152  99208 347259
3   Haemophilus influenzae 1051110 597710      0      0       0      0      0      0   2878
4      Streptococcus mitis  382851 246140 103352 211729   86955  18720 142086 416849   3792
5      Gemella haemolysans  109751  63687  15401 159620   15844   1537 194576 691594  64046
6 Streptococcus pneumoniae  180474 106915  78916  85142   49947  10830 313821 300134      0
> head(data_draw)
                   species variable   value
1 Streptococcus salivarius       T1   44092
2      Rothia mucilaginosa       T1  389451
3   Haemophilus influenzae       T1 1051110
4      Streptococcus mitis       T1  382851
5      Gemella haemolysans       T1  109751
6 Streptococcus pneumoniae       T1  180474

> data_drawspecies \<- factor(data_drawspecies, levels = c("Streptococcus salivarius", "Rothia mucilaginosa", "Haemophilus influenzae", "Streptococcus mitis",

"Gemella haemolysans", "Streptococcus pneumoniae", "Streptococcus parasanguinis", "Neisseria mucosa",

"Streptococcus oralis", "Haemophilus parainfluenzae", "Neisseria subflava", "Neisseria sicca", "Fusobacterium nucleatum",

"Prevotella melaninogenica", "Neisseria flavescens", "Veillonella parvula", "Veillonella dispar",

"Gemella sanguinis", "Streptococcus gordonii", "Leptotrichia wadei", "Streptococcus cristatus",

"Streptococcus anginosus", "Gemella morbillorum", "Neisseria meningitidis", "Veillonella atypica",

"Streptococcus sanguinis", "Corynebacterium argentoratense", "Streptococcus australis",

"Lactobacillus crispatus", "others"))#对种类图形元素排序,保证图形是从大向小排。

> ggplot(data_draw, aes(x = variable, y = value)) +

geom_bar(aes(fill = species), stat = "identity", color = NA, size = 0.4,

position = "fill", alpha = 0.90, width = 0.9) +

scale_fill_manual(values = palette) +

labs(y = "Relative abundance", x = "Sample") +

theme(

text = element_text(family = 'serif'),

axis.title = element_text(size = 14, face = "plain", color = "black"),

axis.text = element_text(size = 14, face = "plain", color = "black"),

legend.title = element_text(size = 14, face = "bold", color = "black"),

panel.background = element_blank(),

axis.line = element_line(colour = "black", size = 0.4),

legend.position = "right", # 在这里设置图例位置

) +

scale_y_continuous(expand = c(0, 0)) +

theme(

plot.margin = unit(c(2, 1, 1, 1), "lines"),

axis.ticks.length = unit(0.1, "cm"),

axis.text.x = element_text(margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm")),

axis.text.y = element_text(margin = unit(c(0.5, 0.5, 0.5, 0.5), "cm"))

)

这种方法是直接将数据处理好,再导入R语言的。

相关推荐
让学习成为一种生活方式7 小时前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
有梦想的Frank博士1 天前
R语言*号标识显著性差异判断组间差异是否具有统计意义
开发语言·信息可视化·r语言
逆风远航4 天前
R语言贝叶斯:INLA下的贝叶斯回归、生存分析、随机游走、广义可加模型、极端数据的贝叶斯分析
开发语言·r语言·贝叶斯·生态学·结构方程·环境科学·混合效应
Faxxtty4 天前
【R语言】解决package ‘qvalue’ is not available (for R version 3.6.1)
开发语言·r语言
Cachel wood5 天前
Vue前端框架:Vue前端项目文件目录
java·前端·vue.js·python·算法·r语言·前端框架
邢博士谈科教6 天前
OmicsTools软件和R语言分析环境安装配置答疑汇总最新版
r语言
纪伊路上盛名在6 天前
vscode中提升效率的插件扩展——待更新
linux·服务器·ide·vscode·python·r语言·编辑器
拓端研究室TRL6 天前
银行信贷风控专题:Python、R 语言机器学习数据挖掘应用实例合集:xgboost、决策树、随机森林、贝叶斯等...
python·决策树·机器学习·数据挖掘·r语言
在在进步6 天前
R数据结构&向量基础
数据结构·r语言
琼火hu8 天前
R语言笔记(五):Apply函数
开发语言·笔记·r语言·apply