初识R语言—ggplot2作图
2020-04-03 本文已影响0人
超级无敌大蜗牛
from 阿雷边学边教
-
初识ggplot2
- ggplot2要素主要包括:背景、坐标轴、图形、标题、图例、分面、文本注释
- 一个具有诚意的ggplot2的作图应该是这样子的:
ggplot(data, aes(x,y)) ##初始化图形并指定数据源和作图变量,aes(aesthetics)美学
+ geom_xx() ##指定图形的类型,geom(geometry):几何学
+ annotate() ##添加文本注释
+ labs() ##修改主标题和坐标轴标题
+ facet_grid() + ...
3.核心理念是:将绘图与数据分离
4.ggplot2是按照图层叠加作图的,通过+号叠加,越到后面图层越高
-
作图类型
1.散点图
ggplot(mtcars,aes(wt,mpg)) + geom_point()

2.柱状图
ggplot(mtcars,aes(cyl)) + geom_bar()

#查看cyl里面的数值分布,可以知道cyl里面明明没有3,5,7,但是做出来的柱状图却有,怎么解决哪?
> table(mtcars$cyl)
4 6 8
11 7 14
> class(mtcars$cyl)
[1] "numeric"
#因为cyl是数值型变量,所以做柱状图的时候才会出现横坐标有3,5,7这样的值,因此我们需要将数值型变量numeric变成分组变量factor。
> ggplot(mtcars,aes(factor(cyl))) + geom_bar()

> ggplot(mtcars,aes(factor(cyl),fill=factor(am))) + geom_bar()#将数值型变量am转换成分组变量,根据am对cyl分组

> ggplot(mtcars,aes(factor(cyl),fill=factor(am))) + geom_bar(position = "dodge")
##position="dodge",am分组分为两列显示

> ggplot(mtcars,aes(factor(cyl),fill=factor(am))) + geom_bar(position = "fill")
##position=fill,显示am分组占比

自定义bar的高度
ggplot(data,aes(distance,fill=distance)) +
geom_bar(aes(weight=counts))+facet_grid(.~types) +
scale_fill_manual(values = wes_palette(n=4, name="Chevalier1")) +
theme_bw() +
theme(panel.grid.major = element_blank(), #不显示网格线
panel.grid.minor = element_blank()) #不显示网格线

3.直方图
> ggplot(mtcars,aes(mpg))+geom_histogram()

4.密度图
> ggplot(mtcars,aes(mpg))+geom_density()

> ggplot(mtcars,aes(mpg,color=factor(vs)))+geom_density()

> ggplot(mtcars,aes(mpg,fill=factor(vs)))+geom_density()
#fill指的是分组的填充颜色。而color指的是分组的线条颜色

> ggplot(mtcars,aes(mpg,fill=factor(vs)))+geom_density(alpha=0.3)
#alpha表示透明度

4.箱线图
ggplot(mtcars,aes(factor(vs),mpg))+geom_boxplot()

5.分组作图
#加factor和不加factor的区别
> ggplot(mtcars,aes(wt,mpg,color=qsec))+geom_point()
> ggplot(mtcars,aes(wt,mpg,color=factor(qsec)))+geom_point()


6.分面作图
#单变量分面
> ggplot(mtcars,aes(wt,mpg))+geom_point()+facet_grid(vs~.)#按照vs纵向分割图
> ggplot(mtcars,aes(wt,mpg))+geom_point()+facet_grid(.~vs)#按照vs横向分割


#双变量分面
> ggplot(mtcars,aes(wt,mpg))+geom_point()+facet_grid(am~vs)#按照am和vs两个变量进行分面
> ggplot(mtcars,aes(wt,mpg))+geom_point()+facet_grid(vs~.,scales = "free_y")#释放y轴,意思就是分开的两面的纵坐标根据所在面决定,而不是两个面的纵坐标都一样,感觉没什么太大必要
> ggplot(mtcars,aes(wt,mpg))+geom_point()+facet_grid(.~vs,scales = "free")#自主选择释放x还是y轴


5.小提琴图
library(ggplot2)
library(dplyr)
ggplot(len, aes(x=events, y=len,fill=types)) +
labs(title="Exon length", y=(expression(paste(log[2], '(exon length)', sep = ""))), x="")+
geom_violin(trim=F,color="white") + #绘制小提琴图, “color=”设置小提琴图的轮廓线的颜色(以下设为背景为白色,其实表示不要轮廓线)
#"trim"如果为TRUE(默认值),则将小提琴的尾部修剪到数据范围。如果为FALSE,不修剪尾部。
geom_boxplot(width=0.2,position=position_dodge(0.9))+
stat_compare_means(method = 'wilcox.test',paired = T, label = "p.signif")+
scale_fill_manual(values = c("#56B4E9", "#E69F00"))+ #设置填充的颜色
theme_bw()+ #背景变为白色
theme(plot.title = element_text(hjust = 0.5),##title居中
axis.text.x=element_text(family="scans",size=18),
axis.text.y=element_text(family="scans",size=12),
axis.title.y=element_text(family="scans",size = 20),
panel.border = element_blank(),axis.line = element_line(colour = "black",size=1), #去除默认填充的灰色,并将x=0轴和y=0轴加粗显示(size=1)
legend.text=element_text(face="plain", family="scans", colour="black", size=16),
legend.title=element_text(face="plain", family="scans", colour="black",size=18),
panel.grid.major = element_blank(), #不显示网格线
panel.grid.minor = element_blank()) #不显示网格线

6.美化
形状
geom_xxx(shape=x, size=y), #x=1,2,3...,数值分别代表不同形状, y=1,2,3...,数值越大,尺寸越大
颜色
#color: 描绘点、线以及图形边缘的颜色
#fill:填充图形内部的颜色(诸如柱状图,密度图等)
#指定填充一种颜色:直接在aes外部写color=“某种颜色”
文本注释
annotate("text", x=, y=, label="")
#text表示添加类型为文本
#x,y表示在指定的坐标位置放入文本
#label表示填写的文本内容
标题
labs(title="",x="",y="")
添加线条
geom_vline(xintercept=)#竖线
geom_hline(yintercept=)#水平线
转换x和y轴
coord_flip
调整轴刻度的范围
xlim, ylim
修改轴上的值
scale_x_continuous(breaks=c(),labels=c())
scale_y_continuous(breaks=c(),labels=c())