你永远不知道你的小伙伴会有什么奇奇怪怪的要求,而这些要求能把作为一个菜鸟的你折腾得半死(/ o \)~~~~
这篇文章就是Poesie无数次呕血后的干货总结,给自己留条后路(谁也不知道电脑什么时候会崩溃 / (#_#) /))
本次干货小提纲~~
1.ggplot 的基本绘图与语法(套用公式)
2.如何快速更换图中
3.ggplot中如何改变
4.如何横轴坐标的
5.ggplot中如何改变
一、数据
这个是我作图的一个数据表格
我作图的一个数据表格
二、将数据读入R中,同时打开绘图所需的包
library(ggplot2) #主要绘图包
# install.packages(readxl)
#我的文件是xlsx格式的,电脑要下载R包readxl ,如果电脑已经下载过了,这个代码就可以直接忽略~~
# 读取数据
AA=readxl::read_xlsx("GO(3).xlsx",sheet = 1)
# 设置图形中的字体格式
windowsFonts(myFont = windowsFont("Times New Roman")) #将字体格式改成Times New Roman格式, 环境存在myFont 格式
- 如果电脑已经下载过了R包readxl,代码install.packages(readxl) 就可直接忽略
- 读取数据中sheet = 1是可以去掉的,这个参数是如果你一个excel表格里面好几个Sheet,可以通过修改数字,决定你读取第几个Sheet,一般默认读取第一个
将sheet = 1改成sheet = 3,就是将读取sheet = 1改成读取sheet = 3
- 设置图形中的字体格式,只要是电脑有的格式,都可以通过修改括号中的Times New Roman,修改字体格式
三、绘图
1. 气泡图总汇代码
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) + # 锁定数据框,X轴Y轴数据
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+ #绘制气泡图
coord_flip()+ # 将X轴与Y轴调换
scale_color_gradient(low="yellow",high ="red")+ #颜色范围
theme(legend.title = element_text(size = 15, face = 2))+ #图例标题大小
theme(legend.key.size=unit(1,'cm'))+ #图例标题大小
theme(legend.text = element_text( size = 15,face = 'bold'))+ #图例图形里文字大小
labs(x="Term",y = "RichFactor",title = "GO")+ # X轴、 Y轴 、图的biao
geom_line() +
theme_bw()+
theme(plot.background = element_rect(fill = "White"))+ #图片背景颜色设置
theme(panel.grid.major=element_line(colour="grey")) +
scale_fill_gradient(low = "pink", high = "red")+
theme(axis.title.x = element_text(size = 15))+
theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
theme(axis.text.y = element_text(size = 12, family = "myFont", color =c("red","red","red","red","red","red","red","red","red","red","red","red","red","red","red","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","orange","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue","blue"),face = "bold"))+
theme(plot.title = element_text(size = 15,face = 4, hjust =0.5))
2. 分步解读
(1) ggplot绘图相当于层层叠加(加代码加图层)
ggplot()
绘图打底的页面,背景层
(2)锁定数据框,X、Y轴
初步锁定了X轴与Y轴,由于Term的名字太长了,导致X轴名字密密麻麻挤到了一起,
后面可以通过代码调整
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)
(3)绘制气泡图
Ⅰ.可见这个图十分简略,接下来就是通过添加代码修饰
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)+
geom_point()
Ⅱ.通过修改参数size(大小)color(颜色)来修改图形
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)+
geom_point(size=5,color="red",shape =0)
Ⅲ、通过修改参数shape (形状)来修改图形
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(size=5,color="red",shape = 10)
shape=X ,X是数字1~25,可以任意更改以满足自己的需求
Ⅳ、参数size(大小)color(颜色)shape (形状)进阶
由于我想将气泡图的大小形状颜色均匀表格中数据联系在一起,锁定aes()
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)+
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))
修饰:①size=10*GeneNumber --用点的大小表示GeneNumber 数值大小,但是由于GeneNumber数值有点过小,图形差异不明显,用10 *,将其扩大10倍
②color=-1 * log10(PValue)----给点上颜色,变量锁定PValue,-1 * log10也是数学运算
Ⅴ. 将X轴与Y轴倒过来
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip() #将X轴与Y轴倒过来
Ⅵ.修改变化中的颜色 ---+ scale_color_gradient()
high,low是颜色变化的上下限,通过自己喜好修改颜色
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip() +#将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")
(4)图例修改
Ⅰ.大小
- legend.title 图例的标题 --参数size(大小) face参数可用于使字体变为粗体或斜体,补:可以加参数color颜色的,用法和上面一样
*face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗 - legend.key.size=unit(1,'cm')) 图例里面图形的大小--修改数字调整
- legend.text--- 图例里面图形的文字--参数 size 与 face 应用如上
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))
legend.title
legend.key.size
legend.text
Ⅱ.图例 c9bc03a6a62f45031ca93f9a701fc00.png
位置
通过+ theme(legend.position = "bottom")
“top” | “right” | “bottom” | “left” |
---|---|---|---|
“顶部” | “右侧” | “底部” | “左侧” |
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
theme(legend.position = "left")
Ⅲ. 若想关闭图例
- 关闭全部图例---+theme(legend.position = "none")
- 若想关闭图例标题---+ theme(legend.title = element_blank())
element_blank()----空白的意思 - 关闭对color产生的图例去掉标题+guides(color=guide_legend(title=NULL))
*同理可换成size
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.position = "none")
关闭全部图例
(5)标题
Ⅰ. + labs()函数
x--X轴标题,y-Y轴标题,title --总标题
由图可见我添加了总标题GO,将Y轴坐标由GeneNumber改成了RichFactor,但由于函数 coord_flip()+ #将X轴与Y轴倒过来,所以RichFactor在横坐标位置
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title = "GO")
标题
Ⅱ.添加副标题
+ggtitle("GO","副标题")
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title ="GO")+
ggtitle("GO","副标题")
Ⅲ. 对标题大小及位置调整
函数/参数 | 含义 | size | family | hjust | face | vjust |
---|---|---|---|---|---|---|
plot.title | 标题 | 字 | 字 | 字 | 字 | 字 |
axis.title.x | X轴标题 | 体 | 体 | 体 | 体 | 体 |
axis.title.y | Y轴标题 | 的 | 的 | 的 | 的 | 的 |
axis.text.x | X轴坐标的标题 | 大 | 样 | 位 | 粗 | 高 |
axis.text.y | Y轴坐标的标题 | 小 | 式 | 置 | 细 | 低 |
①参数size与face,用法与上面一样
② family = "myFont"----字体格式用myFont的格式,myFont在最开始设定好了windowsFonts(myFont = windowsFont("Times New Roman"))#将字体格式改成Times New Roman格式
③hjust=0.5 以为这字体居中--GO标题明显居中
hjust=1
hjust=0
④vjust=一个数值,通过数值调整高低,可正可负,但是一般没有需求就不调整
axis.title
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title = "GO")+
theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))
axis.title+axis.text
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title = "GO")+
theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
theme(axis.text.y = element_text(size = 12, family = "myFont", color ="black", face = "bold"))
补充:vjust
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title ="GO")+
theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold',vjust=-6))
下调标题
(6)锁定轴坐标顺序
细心的朋友可能会发现作图表格中有时候Term的顺序与图中的顺序对应不上,ggplot作图时,会按照字母排序,所以对这方面有需求的小伙伴们可以试一下下面的方法锁定横轴坐标顺序
非常抱歉,那个对应不上的数据误删了,这张图就是个表意①先将Term整列因子化,再用levels锁定顺序(即表格中的顺序)
AA$Term<-levels(factor(AA$Term))
绘图代码不变
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title = "GO")+
theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
theme(axis.text.y = element_text(size = 12, family = "myFont", color ="black", face = "bold"))
如果还有其他顺序的需求可以用代码
图形将会按照c("A","B""C",....)排序显示出来
c("A","B""C",....)里面长长的绿色的都是名字,可按需修改,对应上就好
AA$Term= factor(AA$Term,levels = c( "cytosol" ,
"nucleus" ,
"nucleoplasm" ,
"perinuclear region of cytoplasm" ,
"extracellular region" ,
"extracellular space" ,
"cyclin A2-CDK2 complex" ,
"membrane raft" ,
"caveola" ,
"endosome" ,
"proteinaceous extracellular matrix" ,
......... (太多了,就补一一列举了)
"protein homodimerization activity" ,
"type II transforming growth factor beta receptor binding" ), ordered=TRUE)
比较简单的逻辑
(6)关于背景和网格线
①去掉面板背景颜色
+ theme_bw()
前面代码不变
ggplot(data = AA, mapping = aes(x=Term,y=GeneNumber)) +
geom_point(aes(size=10*GeneNumber,color=-1*log10(PValue),shape = factor(Group)))+
coord_flip()+ #将X轴与Y轴倒过来
scale_color_gradient(low="yellow",high ="red")+
theme(legend.title = element_text(size = 15, face = 2))+
theme(legend.key.size=unit(1,'cm'))+
theme(legend.text = element_text( size = 15,face = 'bold'))+
labs(x="Term",y = "RichFactor",title = "GO")+
theme(plot.title = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.x = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.title.y = element_text(size = 15, family = "myFont", hjust =0.5,face = 'bold'))+
theme(axis.text.x = element_text(size = 12, family = "myFont", color = "black", face = "bold"))+
theme(axis.text.y = element_text(size = 12, family = "myFont", color ="black", face = "bold"))+
theme_bw()
②改变面板颜色
+theme(panel.background = element_rect(fill = "blue"))*
前面的代码不变
+theme(panel.background = element_rect(fill = "blue"))
③网格线
网格线有两种类型:主要网格线指示刻度线,主要网格线之间的次要网格线。
主要网格线 + theme(panel.grid.major=element_line(colour="red"))
前面的代码不变
+ theme(panel.grid.major=element_line(colour="red"))
红色
次要网格线 + theme(panel.grid.minor = element_line(color = "green", size = 0.25))
前面的代码不变
+ theme(panel.grid.minor = element_line(color = "green", size =1))
image.png
③填充旁边的颜色
+theme(plot.background = element_rect(fill = "pink"))
前面的代码不变
+theme(plot.background = element_rect(fill = "pink"))
将旁边变成粉色
④边距
参数plot.margin可以处理各种不同的单位(厘米,英寸等)
+ theme(plot.margin = unit(c(1, 2, 1, 5), "cm"))
边距边的顺序是上,右,下,左
前面的代码不变
+ theme(plot.margin = unit(c(1, 5, 1, 5), "cm"))
边距明显变化
以上是Poesie关于ggplot绘图的一小部分总结,不足之处也希望大家指出
网友评论