简介
渐变色柱形图可以通过颜色深浅和柱形高矮表现更丰富的信息。

开始作图
水平柱状图只要在普通柱状图的基础上 + coord_flip()
即可:
library(ggplot2)
testdata <- data.frame(feature = c("X474_PC.32.1p._PC.32.1p.",
"X446_DG.34.1._DG.16.0.18.1.",
"X548_PE.38.1._PE.38.1.",
"X580_PI.36.2._PI.18.1.18.1.",
"X472_PG.34.1._PG.16.0.18.1.",
"X628_PC.40.7p._PC.40.7p.",
"X498_PC.33.1p._PC.33.1p.",
"X438_ArachidylcarnitineAcCa.20.0._ArachidylcarnitineAcCa.20.0.",
"X639_PC.40.4._PC.18.2.22.2.",
"X479_PC.32.0._PC.16.0.16.0."),
importance = c(3.66, 3.08, 2.99, 2.91, 2.83, 2.77, 2.6, 2.59, 2.54, 2.51))
ggplot(testdata, aes(x = feature, y = importance, fill = feature)) +
geom_bar(stat="identity") +
coord_flip()

我们发现,柱子的排列顺序居然和数据的顺序不一致,为此,我们 需要将 “希望按照顺序排列的轴” 强制转换为 factor 类型。转换之后,柱状图的排列顺序和数据顺序一致了:
library(ggplot2)
testdata <- data.frame(feature = c("X474_PC.32.1p._PC.32.1p.",
"X446_DG.34.1._DG.16.0.18.1.",
"X548_PE.38.1._PE.38.1.",
"X580_PI.36.2._PI.18.1.18.1.",
"X472_PG.34.1._PG.16.0.18.1.",
"X628_PC.40.7p._PC.40.7p.",
"X498_PC.33.1p._PC.33.1p.",
"X438_ArachidylcarnitineAcCa.20.0._ArachidylcarnitineAcCa.20.0.",
"X639_PC.40.4._PC.18.2.22.2.",
"X479_PC.32.0._PC.16.0.16.0."),
importance = c(2.51, 2.54, 2.59, 2.6, 2.77, 2.83, 2.91, 2.99, 3.08, 3.66))
testdata[["feature"]] = factor(testdata[["feature"]], levels = as.character(testdata[["feature"]]))
ggplot(testdata, aes(x = feature, y = `importance`, fill = feature)) +
geom_bar(stat="identity") +
coord_flip()

柱状图的默认配色略显浮夸,不够学术,我们调整一下颜色,设置从上至下的渐变风格。
在此之前,需要安装调色板依赖包:
install.packages("RColorBrewer")
install.packages("remotes")
remotes::install_github("eprifti/momr")
library(ggplot2)
library(RColorBrewer)
library(momr)
testdata <- data.frame(feature = c("X474_PC.32.1p._PC.32.1p.",
"X446_DG.34.1._DG.16.0.18.1.",
"X548_PE.38.1._PE.38.1.",
"X580_PI.36.2._PI.18.1.18.1.",
"X472_PG.34.1._PG.16.0.18.1.",
"X628_PC.40.7p._PC.40.7p.",
"X498_PC.33.1p._PC.33.1p.",
"X438_ArachidylcarnitineAcCa.20.0._ArachidylcarnitineAcCa.20.0.",
"X639_PC.40.4._PC.18.2.22.2.",
"X479_PC.32.0._PC.16.0.16.0."),
importance = c(2.51, 2.54, 2.59, 2.6, 2.77, 2.83, 2.91, 2.99, 3.08, 3.66))
testdata[["feature"]] = factor(testdata[["feature"]], levels = as.character(testdata[["feature"]]))
cols<-brewer.pal(3, "YlOrRd")
pal<-colorRampPalette(cols)
mycolors<-pal(nrow(testdata))
ggplot(testdata, aes(x = feature, y = `importance`, fill = feature)) +
geom_bar(stat="identity") +
coord_flip() +
scale_fill_manual(values = mycolors)

去掉背景色和网格线:
library(ggplot2)
library(RColorBrewer)
library(momr)
testdata <- data.frame(feature = c("X474_PC.32.1p._PC.32.1p.",
"X446_DG.34.1._DG.16.0.18.1.",
"X548_PE.38.1._PE.38.1.",
"X580_PI.36.2._PI.18.1.18.1.",
"X472_PG.34.1._PG.16.0.18.1.",
"X628_PC.40.7p._PC.40.7p.",
"X498_PC.33.1p._PC.33.1p.",
"X438_ArachidylcarnitineAcCa.20.0._ArachidylcarnitineAcCa.20.0.",
"X639_PC.40.4._PC.18.2.22.2.",
"X479_PC.32.0._PC.16.0.16.0."),
importance = c(2.51, 2.54, 2.59, 2.6, 2.77, 2.83, 2.91, 2.99, 3.08, 3.66))
testdata[["feature"]] = factor(testdata[["feature"]], levels = as.character(testdata[["feature"]]))
cols<-brewer.pal(3, "YlOrRd")
pal<-colorRampPalette(cols)
mycolors<-pal(nrow(testdata))
ggplot(testdata, aes(x = feature, y = `importance`, fill = feature)) +
geom_bar(stat="identity") +
coord_flip() +
scale_fill_manual(values = mycolors) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())

去掉 legend,去掉 x 轴 label:
library(ggplot2)
library(RColorBrewer)
library(momr)
testdata <- data.frame(feature = c("X474_PC.32.1p._PC.32.1p.",
"X446_DG.34.1._DG.16.0.18.1.",
"X548_PE.38.1._PE.38.1.",
"X580_PI.36.2._PI.18.1.18.1.",
"X472_PG.34.1._PG.16.0.18.1.",
"X628_PC.40.7p._PC.40.7p.",
"X498_PC.33.1p._PC.33.1p.",
"X438_ArachidylcarnitineAcCa.20.0._ArachidylcarnitineAcCa.20.0.",
"X639_PC.40.4._PC.18.2.22.2.",
"X479_PC.32.0._PC.16.0.16.0."),
importance = c(2.51, 2.54, 2.59, 2.6, 2.77, 2.83, 2.91, 2.99, 3.08, 3.66))
testdata[["feature"]] = factor(testdata[["feature"]], levels = as.character(testdata[["feature"]]))
cols<-brewer.pal(3, "YlOrRd")
pal<-colorRampPalette(cols)
mycolors<-pal(nrow(testdata))
ggplot(testdata, aes(x = feature, y = `importance`, fill = feature)) +
geom_bar(stat="identity") +
coord_flip() +
scale_fill_manual(values = mycolors) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank()) +
theme(legend.position = 'none') +
xlab("")

看看其他渐变色,有没有你中意的款:

欢迎留言、讨论、点赞、转发,转载请注明出处~
相关文章
[1] R 数据可视化:BoxPlot
[2] R 数据可视化:双坐标系柱线图
[3] R 数据可视化:PCA 主成分分析图
[4] R 数据可视化:环形柱状图
网友评论