R语言基础图形绘制——条形图



简介

  • 条形图也许是最常用的数据可视化方法,通常用来展示不同的分类下(在 x 轴上)某个数值型变量的取值(在 y 轴上)。


1. 基础函数barplot()绘制条形图

(1) R包安装
# 首先绘制类风湿性关节炎新疗法研究的结果,该数据包含于vcd包中。
if(!require("vcd")) install.packages("vcd")
# 因为我已经安装过vcd包,所以上面的命令会直接加载vcd包,否则,需要载入vcd包
# suppressPackageStartupMessages(library(vcd))
(2) Barplot
Barplot 1
improved <- table(Arthritis$Improved)
improved
# None   Some Marked 
#    42     14     28 
barplot(improved,main = "simple barplot",xlab = "improvement",ylab = "frequency")

在这里插入图片描述

Barplot 2
barplot(improved,main = "simple barplot",
        xlab = "improvement",ylab = "frequency",
        horiz = T)

在这里插入图片描述

Barplot 3
# 对柱子进行排序
data <- as.data.frame(improved) # 将table转换成data frame
data <- data[order(data$Freq),]
data$Var1 <- factor(data$Var1,levels = data$Var1)
barplot(data$Freq ~ data$Var1, main = "sorted barplot",
        xlab = "improvement",ylab = "frequency",space = .4) # space参数控制柱子间距

在这里插入图片描述

Barplot 4
barplot(data$Freq ~ data$Var1, main = "sorted barplot",
        xlab = "improvement",ylab = "frequency",space = .4,
        col = "steelblue",border = "red")

在这里插入图片描述

Barplot 5(堆砌条形图)

注意:分组的柱状图不一定必须是table类型,matrix也可以,举一个简单的小例子:

a <- c(26.74,17.55,37.03,18.68)
data <- matric(a,nrow = 2)
barplot(data,beside = T,names.arg = c('group1','group2'))

在这里插入图片描述

# 堆砌条形图
improved <- table(Arthritis$Improved, Arthritis$Treatment)
improved
#        Placebo Treated
#  None        29      13
#  Some         7       7
#  Marked       7      21
barplot(improved, 
        main="Stacked Bar Plot",ylab="Frequency", 
        col=c("red","green","steelblue"),xlim = c(0,3)) 
legend("topright", rownames(improved), cex = 0.8, fill = c("red","green","steelblue"))

在这里插入图片描述

Barplot 6(分组条形图)
# 分组条形图
barplot(improved, 
        main="Grouped Bar Plot", 
        xlab="Treatment", ylab="Frequency", 
        col=c("red","green","steelblue"),beside=TRUE)
legend("top", rownames(improved), cex = 0.8, fill = c("red","green","steelblue"))

在这里插入图片描述

# 更多barplot()函数的参数设置,请参考barplot官方说明文档
# help(barplot)
# ?barplot

2. ggplot函数绘制条形图

(1) R包安装并加载
if(!require("gcookbook")) install.packages("gcookbook")
if(!require("ggplot2")) install.packages("ggplot2")
library(gcookbook) # 加载数据包
library(ggplot2)
(2) ggplot_bar
ggplot_bar 1
# 柱子排序参考基础函数的方法
ggplot(pg_mean, aes(x=group, y=weight)) + 
  geom_bar(stat="identity",width = .5,col = "red",lwd = 1,fill = "steelblue")+
  theme_classic()+ #坐标轴风格修改
  theme(axis.line.x= element_line(size = 1.5),
        axis.line.y= element_line(size = 1.5)) # 修改主题

在这里插入图片描述

ggplot_bar 2
ggplot(pg_mean, aes(x=group, y=weight)) + 
  geom_bar(stat="identity",width = .5,col = "red",lwd = 1,fill = c("gray","green","steelblue"))+ 
  # lwd可以改变柱子border的宽度
  theme_classic()+
  scale_x_discrete(expand = c(0,0))+ # x轴为非连续型变量时,起始柱子位置设置
  scale_y_continuous(expand = c(0,0),limits = c(0,6))+ # 修改y轴
  theme(axis.line.x= element_line(size = 1.5),
        axis.line.y= element_line(size = 1.5)) # 修改主题

在这里插入图片描述

ggplot_bar 3(分组条形图)
#  分组柱形图
cabbage_exp # 查看数据
 Cultivar Date Weight        sd  n         se
#     c39  d16   3.18 0.9566144 10 0.30250803
#     c39  d20   2.80 0.2788867 10 0.08819171
#     c39  d21   2.74 0.9834181 10 0.31098410
#     c52  d16   2.26 0.4452215 10 0.14079141
#     c52  d20   3.11 0.7908505 10 0.25008887
#     c52  d21   1.47 0.2110819 10 0.06674995
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
  geom_bar(stat="identity",position=position_dodge(0.7),width = .6)+ 
  theme_bw()

在这里插入图片描述

ggplot_bar 4(堆砌条形图)

# 堆砌柱形图
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
  geom_bar(stat="identity",width = .6,lwd = 1,col = "black")+ 
  theme_bw()

在这里插入图片描述
补充: 对正负取值的条形图分别着色。

library(gcookbook) # 数据包
csub <- climate %>% subset(Source=="Berkeley" & Year >= 1900)
csub$pos <- csub$Anomaly10y >=0
ggplot(csub, aes(x=Year, y=Anomaly10y, fill=pos))+
  geom_bar(stat="identity", position="identity",col = "black",size = 0.5)+
  scale_fill_manual(values = c("blue","red"),guide = F)+
  theme_void()

在这里插入图片描述

3. 学以致用

  • 读入基因表达数据,计算标准差(SD),并对数据进行处理。
  • 并且在此部分中,对柱子添加了误差线。
bar <- read.csv("bar.csv",header = T)
bar$ctl <- (bar$fC.1 + bar$fC.2) / 2 
bar$exp <- (bar$fC.kd1 + bar$fC.kd2) / 2 
rownames(bar) <- bar$genename 
bar <- bar[,-1]
bar  <- log2(bar + 1)
bar$exp <- bar$exp /bar$ctl
bar$ctl <- bar$ctl / bar$ctl
bar$fC.kd1 <- bar$fC.kd1 / bar$fC.2
bar$fC.kd2 <- bar$fC.kd2 / bar$fC.1
bar$fC.1 <- bar$fC.1 / bar$fC.1
bar$fC.2 <- bar$fC.2 / bar$fC.2

fpkm <- c(bar$ctl,bar$exp)
gene <- rep(rownames(bar),2)
a <- apply(bar[,1:2], 1, sd)
b <- apply(bar[,3:4], 1, sd)
sd <- c(a,b)
group <- c(rep('control',10),rep('experiment',10))
fig <- data.frame(fpkm,gene,sd,group)
fig
fig$gene = factor(fig$gene, levels=c('Pou5f1','Eif4g1','Tead4','Yap1',
                                     'Yy1','Gata4','Ctnnb1','Klf2',
                                     'Pnn','Zfp553'))
1. 绘制基因表达量FPKM的相对值
library(ggpubr)
windowsFonts(myFont=windowsFont("Arial"))
ggplot(fig, aes(x=gene, y=fpkm, fill=group)) + 
  geom_bar(stat="identity", position=position_dodge(0.9),
           width = 0.6,
           color = 'black',size = 1) +
  geom_errorbar(aes(ymin=fpkm, ymax=fpkm+sd), width=.2,
                size = 1,
                position=position_dodge(.9))+
  #stat_compare_means(aes(group = gene),label.y = 1.10) +
  scale_fill_brewer(palette="Paired") + 
  xlab(label = "") +
  ylab(label = "") +
  scale_y_continuous(limits = c(0,1.15),
                     breaks = seq(0,1.15,0.25),
                     expand = c(0,0)) +
  theme_classic(base_line_size = 1,base_rect_size = 1)+
  theme(axis.text.x = element_text(angle = 45,size = 11,
                                   family="myFont",face = 'italic',
                                   hjust = 0.5,vjust = 0.5),
        axis.text.y = element_text(family="myFont",
                                   size = 11))

在这里插入图片描述

2. 绘制挑选基因表达量FPKM的相对值
fig2 <- fig[fig$gene == 'Yap1' | fig$gene == 'Tead4',]
fig2$gene = factor(fig2$gene,levels = c('Yap1','Tead4'))
ggplot(fig2, aes(x=gene, y=fpkm, fill=group)) + 
  geom_bar(stat="identity", position=position_dodge(0.9),
           width = 0.6,
           color = 'black',size = 1) +
  geom_errorbar(aes(ymin=fpkm, ymax=fpkm+sd), width=.2,
                size = 1,
                position=position_dodge(.9))+
  #stat_compare_means(aes(group = gene),label.y = 1.10) +
  scale_fill_brewer(palette="Paired") + 
  xlab(label = "") +
  ylab(label = "") +
  scale_y_continuous(limits = c(0,1.15),
                     breaks = seq(0,1.15,0.25),
                     expand = c(0,0)) +
  theme_classic(base_line_size = 1,base_rect_size = 1)+
  theme(axis.text.x = element_text(angle = 45,size = 11,
                                   family="myFont",face = 'italic',
                                   hjust = 0.5,vjust = 0.5),
        axis.text.y = element_text(family="myFont",
                                   size = 11))

在这里插入图片描述


  • ##侵权请联系作者删除!

参考书籍

[1] 《R数据可视化手册》

[2] 《R语言实战》(第二版)

  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用R语言绘制复式条形图,可以按照以下步骤操作: 1. 载入所需的包,如ggplot2: ```R library(ggplot2) ``` 2. 使用ggplot函数创建图形对象,并设置数据来源和映射参数,其中x轴变量为因子型变量(cyl),并使用fill参数将颜色映射到因子变量(cyl): ```R ggplot(data=mtcars, mapping=aes(x=as.factor(cyl),fill=as.factor(cyl))) ``` 3. 使用geom_bar函数添加条形图层,设置stat参数为"count"表示绘制计数条形图,width参数控制条形宽度: ```R geom_bar(stat="count",width=0.5) ``` 4. 使用scale_fill_manual函数设置条形图颜色,其中values参数设置颜色值,以"#RRGGBBAA"格式表示: ```R scale_fill_manual(values=c("#3C5488B2","#00A087B2","#F39B7FB2")) ``` 5. 使用geom_text函数添加条形图的文本,设置stat参数为"count"表示绘制计数条形图,aes函数设置label参数为..count..表示显示计数值,vjust参数控制文本位置,color参数设置文本颜色,size参数设置文本大小: ```R geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5) ``` 6. 使用theme_minimal函数设置图形主题为最小化风格: ```R theme_minimal() ``` 7. 如果希望将x轴和y轴互换,可以使用coord_flip函数: ```R coord_flip() ``` 综合以上步骤,可以使用以下代码绘制复式条形图: ```R library(ggplot2) ggplot(data=mtcars, mapping=aes(x=as.factor(cyl),fill=as.factor(cyl))) + geom_bar(stat="count",width=0.5) + scale_fill_manual(values=c("#3C5488B2","#00A087B2","#F39B7FB2")) + geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5) + theme_minimal() + coord_flip() ```<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [R语言绘制条形图](https://blog.csdn.net/weifanbio/article/details/116784420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值