前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言基础绘图教程——第5章:直方图和柱状图

R语言基础绘图教程——第5章:直方图和柱状图

作者头像
DoubleHelix
发布2019-08-15 17:24:11
5.6K0
发布2019-08-15 17:24:11
举报
文章被收录于专栏:生物信息云生物信息云
R基础教程可先阅读:R语言编

1 barplot()函数绘制

数据:

Group Count1 Count2

Control 10 8

Drug1 28 13

Drug2 23 14

Drug3 9 18

Drug4 15 6

代码语言:javascript
复制
#读入数据
data = read.table("barplot.txt",header=T)
#绘制条形图,仔细喊下面没一行代码都生成一个图,看他们的差别会知道参数是干嘛的。
barplot(data[,2])
barplot(data[,2],names.arg = data[,1])
barplot(data[,2],names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量")
barplot(data[,2],names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量",col="blue")
barplot(data[,2],names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量",col=c("grey","red","blue","orange","green")
代码语言:javascript
复制
barplot(data[,2])
代码语言:javascript
复制
# 添加横轴分组名称
barplot(data[,2],names.arg = data[,1])
代码语言:javascript
复制
# 添加横纵坐标的名称和图片名称
barplot(data[,2],names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量")
代码语言:javascript
复制
# 更改条形颜色
barplot(data[,2],names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量",col="blue")
代码语言:javascript
复制
为不同的组设置不同的颜色
barplot(data[,2],names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量",col=c("grey","red","blue","orange","green"))

多种分组的柱状图:堆积柱状图

代码语言:javascript
复制
#转换数据
data2 = t(data[,c(2,3)])
#绘制柱状图
barplot(as.matrix(data2))
代码语言:javascript
复制
#调整图形
barplot(as.matrix(data2),
        names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量",
        col=c("blue","red"),
        legend=c("Low Dose","High Dose"),
        ylim=c(0,50))
box() #边框
代码语言:javascript
复制
###------水平柱状图
par(las=2)#调整水平轴数字方向
barplot(as.matrix(data2),
        names.arg = data[,1],main="条形图",xlab="统计量",
        col=c("blue","red"),
        legend=c("Low Dose","High Dose"),
        xlim=c(0,50),
        horiz=TRUE,
        space=0.5)

box() #边框

多种分组的柱状图:非堆积柱状图

代码语言:javascript
复制
#非堆积柱状图
barplot(as.matrix(data2),
        names.arg = data[,1],main="条形图",xlab="分组",ylab="统计量",
        col=c("blue","red"),
        legend=c("Low Dose","High Dose"),
        ylim=c(0,30),
        beside=TRUE)
box() #边框

2

ggplot2绘制

部分数据:

Source Year Anomaly10y Unc10y

Berkeley 1900 -0.171 0.108

Berkeley 1901 -0.162 0.109

Berkeley 1902 -0.177 0.108

Berkeley 1903 -0.199 0.104

Berkeley 1904 -0.223 0.105

Berkeley 1905 -0.241 0.107

Berkeley 1906 -0.294 0.106

Berkeley 1907 -0.312 0.105

绘图格式:

代码语言:javascript
复制
ggplot(data,aes())+ geom_bar()
代码语言:javascript
复制
library(ggplot2)
csub <- read.table("barplot_using_ggplot2.txt", sep="\t", header=T)
head(csub)
ggplot(csub,aes(x=Year,y=Anomaly10y))+
  geom_bar(stat="identity",position="identity")

修改柱子颜色,正负值用不同的颜色

代码语言:javascript
复制
#positive and negative values with different color
csub$positive <- csub$Anomaly10y >= 0
ggplot(csub,aes(x=Year,y=Anomaly10y,fill=positive))+
  geom_bar(stat="identity",position="identity")
代码语言:javascript
复制
#change more params
ggplot(csub, aes(Year,Anomaly10y, fill=positive))+
  geom_bar(stat="identity", position="identity", colour="black", size=0.25) +
  scale_fill_manual(values=c("#CCEEFF", "#FFDDDD"), guide=F)

条形图

代码语言:javascript
复制
library(ggplot2)
#readin data
data = read.table("histogram1.txt", sep="\t", header=T)
#draw histogram 
ggplot(data, aes(waiting)) +
  geom_histogram()
#set binwidth 5
ggplot(data, aes(waiting)) +
  geom_histogram(binwidth=5, fill="white", color="black")
#how to set the binwidth to make the histogram have 15 groups?
binsize <- diff(range(data$waiting))/15
ggplot(data, aes(waiting)) +
  geom_histogram(binwidth=binsize, fill="white", color="black")

#set the value of "origin"
ggplot(data, aes(waiting)) +
  geom_histogram(binwidth=8, fill="white", color="black", origin=30)
ggplot(data, aes(waiting)) +
  geom_histogram(binwidth=8, fill="white", color="black", origin=40)
代码语言:javascript
复制
library(ggplot2)

#readin data
data = read.table("histogram2.txt", sep="\t", header=T)

#draw histogram with 
ggplot(data, aes(bwt)) +
  geom_histogram()

#draw histogram with in different subset
data$smoke = factor(data$smoke)
ggplot(data, aes(bwt, fill=smoke)) +
  geom_histogram(position="identity", alpha=0.4)
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MedBioInfoCloud 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 barplot()函数绘制
  • 多种分组的柱状图:堆积柱状图
  • 多种分组的柱状图:非堆积柱状图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档