ggplot绘图(气泡图)(一)

你永远不知道你的小伙伴会有什么奇奇怪怪的要求,而这些要求能把作为一个菜鸟的你折腾得半死(/ o \)~~~~
这篇文章就是Poesie无数次呕血后的干货总结,给自己留条后路(谁也不知道电脑什么时候会崩溃 / (#_#) /))

本次干货小提纲~~

1.ggplot \color{red}{气泡图}的基本绘图与语法(套用公式)

2.如何快速更换图中\color{red}{文字字体样式}

3.ggplot中如何改变\color{red}{主题的字体、颜色、大小、位置(居中等)}

4.如何\color{red}{锁定}横轴坐标的\color{red}{标签顺序}

5.ggplot中如何改变 \color{red}{背景颜色,网格等}

一、数据

这个是我作图的一个数据表格


我作图的一个数据表格

二、将数据读入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 格式
  1. 如果电脑已经下载过了R包readxl,代码install.packages(readxl) 就可直接忽略
  2. 读取数据中sheet = 1是可以去掉的,这个参数是如果你一个excel表格里面好几个Sheet,可以通过修改数字,决定你读取第几个Sheet,一般默认读取第一个
    \color{red}{例如:AA=readxl::read_xlsx("GO(3).xlsx",sheet = 3)}\
    将sheet = 1改成sheet = 3,就是将读取sheet = 1改成读取sheet = 3
  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也是数学运算

③shape = factor(Group)----根据Group列来改变其形状的不同,注意要将分组的哪一列factor()因子化
Ⅴ. 将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)图例修改

Ⅰ.大小

  1. legend.title 图例的标题 --参数size(大小) face参数可用于使字体变为粗体或斜体,补:可以加参数color颜色的,用法和上面一样
    *face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗
  2. legend.key.size=unit(1,'cm')) 图例里面图形的大小--修改数字调整
  3. 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")


Ⅲ. 若想关闭图例

  1. 关闭全部图例---+theme(legend.position = "none")
  2. 若想关闭图例标题---+ theme(legend.title = element_blank())
    element_blank()----空白的意思
  3. 关闭对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绘图的一小部分总结,不足之处也希望大家指出

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342