R语言画图-条形图,堆叠条形图(ggplot2)

Python021

R语言画图-条形图,堆叠条形图(ggplot2),第1张

1. barplot函数

>a=matrix(1:18,2)

>a

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]

[1,]    1    3    5    7    9   11   13   15   17

[2,]    2    4    6    8   10   12   14   16   18

>class(a) #查看a数据类型

[1] "matrix"

注意barplot函数对象要么是向量,要么是矩阵,若不是,则要进行数据数据类型进行转换

>barplot(d)  #所有参数默认

>?barplot

常见参数就不赘述了,几个个人认为比较重要参数如下

names.arg----在每个条形图或条形图下绘制的名称向量。 如果省略此参数,那么如果它是向量,则从height的names属性中获取名称;如果它是矩阵,则从列名称中获取名称。

legend.text----数据为矩阵的时候用,如果legend.text为true,则height的行名称非空时将用作标签。

horiz----默认false,为竖直条形图,改为TRUE,为水平条形图

beside---如果为FALSE,则将高度列描绘为堆叠的条,如果为TRUE,则将列描绘为并列的条

space---每根柱子之前留出的空间量(以平均柱子宽度的一部分为单位)。 可以以单个数字或每个小节一个数字的形式给出。 如果height是一个矩阵,并且next为TRUE,则可以用两个数字指定空间,其中第一个是同一组中的条形之间的间隔,第二个是组之间的间隔。 如果未明确给出,则如果height为矩阵,并且next为TRUE,则默认为c(0,1),否则为0.2。

还有很多参数可以通过help()查询

>barplot(a,names.arg = c('1','2','3','4','5','6','7','8','9'),beside = TRUE,horiz = TRUE,col = rep(c('blue','green','gray'),3),legend.text = TRUE)

>barplot(a,names.arg = c('1','2','3','4','5','6','7','8','9'),beside = F,horiz = TRUE,col = rep(c('blue','green'),2),legend.text = TRUE)

>barplot(a,names.arg = c('1','2','3','4','5','6','7','8','9'),beside = F,horiz = F,col = rep(c('blue','green'),2),legend.text = TRUE) 

2.ggplot2包

安装加载包

install.package('ggplot2')

library(ggplot2)

#创建矩阵

data<-data.frame(Sample<-c(rep('control1',3),rep('control2',3),rep('control3',3),rep('treat1',3),rep('treat2',3),rep('treat3',3),rep('treat4',3)), contion<-rep(c('Cell','Tissue','Organ'),7), value<-c(503,264,148,299,268,98,363,289,208,108,424,353,1,495,168,152,367,146,48,596,143))

colnames(data)=c('sample',"contion","value")

ggplot(data,mapping = aes(Sample,value,fill=contion))+geom_bar(stat='identity',position='fill') +labs(x = 'Sample',y = 'frequnency') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'))+theme(axis.text.x = element_text(angle = 45, hjust = 1))

#ggplot函数,geom从数据到几何图像,geom_bar为柱状图,geom_line为线型图等,aes形成映射,x轴为sample,y轴为value,堆叠为contion,geom_bar()函数为建立柱状图,stat参数-统计变换,position参数为柱状图形式,position= 'fill'(图形元素堆叠且高度标准化为1),position= 'stack'(图形堆叠图),参数position= 'dodge'(并列数据,非堆叠展示),coord画图在某个坐标系中,facet将绘图窗口分成若干子窗口用来生成数据中不同子集的图形

# labs为标题,theme为设置标题参数,axis.title为轴标题信息,axis.text为轴注释文本,axis.text.x表示设置x轴的信息,还有更多参数详查ggplot2包

ggplot(data,mapping = aes(Sample,value,fill=contion))+geom_bar(stat='identity',position='fill') +labs(x = 'Sample',y = 'frequnency') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'))+theme(axis.text.x = element_text(angle = 45, hjust = 1))+coord_flip() #加的函数可实现水平柱状图展示

ggplot(data,mapping = aes(Sample,value,fill=contion))+geom_bar(stat='identity',position='stack') +labs(x = 'Sample',y = 'frequnency') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'))+theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplot(data,mapping = aes(Sample,value,fill=contion))+geom_bar(stat='identity',position='dodge') +labs(x = 'Sample',y = 'frequnency') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'))+theme(axis.text.x = element_text(angle = 45, hjust = 1))

画出的图上 工具栏上有个向左的 黑色箭头 点击下 就可以对图形编辑了。点击菜单栏insert 选择text 在点击你要的地方 就可以了。

如果在编程中 可以 text(a,b, 'c ')

a为X的坐标

b为Y 的坐标

c为你要的写的字

R语言绘图系列:

根据cyl变量把图形分成4列

根据drv变量把图形分成3行

每个小图x轴和y轴的轴标签完全一致,会导致极值显示不出来

使用scales参数释放坐标轴刻度

2.1 grid.arrange函数

生成四张图

将四张图拼到一张图

2.2 ggrepel包 在图上添加文本,可以自动把叠加的文本分开。

geom_text_repel函数