数据:
Group Count1 Count2
Control 10 8
Drug1 28 13
Drug2 23 14
Drug3 9 18
Drug4 15 6
#读入数据
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")
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"))
#转换数据
data2 = t(data[,c(2,3)])
#绘制柱状图
barplot(as.matrix(data2))
#调整图形
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() #边框
###------水平柱状图
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() #边框
#非堆积柱状图
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
绘图格式:
ggplot(data,aes())+ geom_bar()
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")
修改柱子颜色,正负值用不同的颜色
#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")
#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)
条形图
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)
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)
本文分享自 MedBioInfoCloud 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!