生信星球培训第十九期

学习小组Day6笔记--尹露茜

2019-07-07  本文已影响0人  尹露茜

把豆豆花花的写的ggplot相关教程整理了一下
并没有完全消化
列了份整体大纲
存储了代码备用

屏幕快照 2019-07-07 上午10.26.11.png

变量

离散型变量
连续型变量

作图先要指定使用哪个数据集,然后指定xy轴。

(1)图层:不同的几何对象、图形元素的叠加方式,代码定义图层之间 用+连接

个体几何对象:
群组几何对象:需要设置group,默认是使用过的离散型变量

(2)aes(映射):建立数据与几何图形之间的关系

颜色类:color(颜色或边框颜色)、fill(填充颜色)和 alpha(透明度)
形状类:linetype(线型)、size(点的大小或线的宽度)和 shape(形状)
位置类:x, y, xmin, xmax, ymin, ymax, xend, yend
特殊类:一类是group和order,另一类是字符串映射

(3)几何对象:对映射的渲染或展示

geom_point:点图
geom_line:折线图
geom_area:面积图
geom_bar:直方图

绘图代码

p_plot <- ggplot(data=dexp,aes(x=Gene,y=Expression,color=Sample))+geom_point() #点图

p_line <- ggplot(data=dexp,aes(x=Sample,y=Expression,color=Gene))+geom_line(aes(group=Gene))#right #折线图

p_boxplot <- ggplot(data=dexp,aes(x=Gene,y=Expression,color=Gene))+geom_boxplot()#箱线图

p_density <- ggplot(data = dexp)+geom_density(aes(Expression, color = Gene)) #密度图

p_violin <-ggplot(data = dexp,aes(x = Sample, y = Expression)) + geom_violin(aes(fill = Group,color=Group))#小提琴图

grid.arrange(p_plot,p_line,p_density, p_boxplot, p_violin,nrow=3)

分面的函数:指的是同一类型的图(比如都是点图,都是折线图)分好几个小图,而不是不同类型的图拼在一起
facet_wrap

facets: 分面参数如 ~Group,表示用 Group 变量进行数据分类
nrow: 绘制图形的行数
ncol: 绘制图形的列数,nrow/ncol只设定一个即可

scales

fixed小图均使用统一坐标;
free每个小图按照各自数据范围自由调整坐标;
free_x为自由调整x轴刻度范围;
free_y为自由调整y轴刻度范围。
ps<-ggplot(data = dexp, aes(x = Sample, y = Expression)) ps + geom_point(aes(color=Gene)) + facet_wrap(~Group,scales = "free_x") #把多余的坐标删掉用scales

facet_grid 与facet_wrap不同的重要参数

facets: 应用两个标准分面,如Gene ~ Group
dexp_small<-filter(dexp, Gene %in% paste("G", 1:4, sep = "")) psm<-ggplot(data = dexp_small, aes(x = Sample, y = Expression)) psm + geom_point(aes(color=Length)) + facet_grid(Gene ~ Group, scales = "free_x")

margins: Ture,包含所有数据的组
dexp_small<-filter(dexp, Gene %in% paste("G", 1:4, sep = "")) psm<-ggplot(data = dexp_small, aes(x = Sample, y = Expression)) psm + geom_point(aes(color=Length)) + facet_grid(Gene ~ Group, scales = "free_x", margins = T,space = "free")

space: 每张小图的幅宽,可设置为固定(fixed)和自由(free,free_x,free_y)

用bins定义窗口数量(窗口=图上的柱子)

ph <- ggplot(data = gene_len,aes(fill=..count..)) ph + geom_bar(stat = "bin",#stat=“bin”可以省略,因为是默认值 bins = 25, aes(x = Length, y = ..count..)) #直方图geom_histogram #在直方图中指定统计方式是按照窗口进行统计 #..count..是ggplot 里的特殊变量,代表统计变换里每个窗口里的计数。

按照binwidth定义

ph <- ggplot(data = gene_len,aes(fill=..count..)) ph + geom_bar(stat = "bin", binwidth = 1000, #把这里去掉一个0就是100 aes(x = Length, y = ..count..))

统计变换与几何对象

 ph + stat_bin(binwidth = 100, geom = "bar" ,#bar代表直方图。可省略,因为是默认值aes(x = Length, y = ..count..))
##定义背景图层
 p <- ggplot(data = dexp_small, aes(x = Sample, y = Expression))
##绘制各类图形
    *   #geom_point:散点图
    *   p_point <-p + geom_point(
    *   stat = "identity",
    *   aes(color = Gene),
    *   ) +
    *   labs(title = "geom_point")
    *   #geom_bar:条形图
    *   p_bar1<-p + geom_bar(
    *   stat = "identity",
    *   aes(fill = Gene), #fill是填充颜色,改成color就是边框颜色了,默认填充黑色
    *   )
    *   p_bar2 <- p + geom_bar(
    *   stat = "identity",
    *   aes(fill = Gene),
    *   position = "dodge" #默认stack,改为dodge则是并排,fill将总和算作一,显示百分比。
    *   )
    *   #geom_line:折线图
    *   p_line <- p + geom_line(
    *   stat = "identity",
    *   aes(color = Gene, group = Gene)
    *   )
    *   #geom_area:面积图 #折线图的积分
    *   p_area <- p + geom_area(
    *   stat = "identity",
    *   aes(fill = Gene, group = Gene),
    *   position = "dodge",  #改成“stack”,则会层叠起来
    *   alpha = 5/10 #产生遮挡,调整透明度
    *   )
    *   #geom_boxplot:箱线图
    *   p_boxplot <- p + geom_boxplot(
    *   stat = "boxplot",
    *   aes(color = Sample, group = Sample)
    *   )
    *   ##密度图
    *   p_density <- ggplot(data = dexp_small)+
    *   geom_density(stat = "density",
    *   aes(Expression, color = Gene))
    *   ##小提琴图
    *   p_violin <-ggplot(data = dexp_small,aes(x = Sample, y = Expression)) + geom_violin(stat = "ydensity",
    *   aes(fill = Sample,color=Sample))

合并显示在同一张图上-grid.arrange

 grid.arrange(p_density, p_boxplot, p_violin)
 grid.arrange(p_point, p_bar1,p_bar2, p_line, p_area,p_boxplot,p_violin, p_density,ncol = 2)#两列

引导元素是图例和坐标轴的统称

        *   ppname <- pp+ scale_x_discrete(name = "doudouGene") +
        *   scale_y_continuous(name = "huahuaExpression") +
        *   scale_color_hue(name = "doudouSample") +
        *   scale_size_continuous(name = "huahualength")

修改2:修改引导元素标签:参数lables
pplabel <- pp + scale_x_discrete(labels = c(1:40))

初阶:修改x轴标签(也就是坐标)
中阶:修改其他标签。这里涉及到一个必会的知识点:离散型变量和连续型变量(discrete),通俗的解释就是字符算是离散型,数字算是连续型(continuous)y轴坐标是数字,所以只能改数字的格式,修改连续型变量需要加载一个scales包。有四种格式可以改:
* comma 每隔三位数显示逗号的格式
* percent 百分数
* dollar 美元
* scientific 科学计数法

修改3:定义域和值域,参数limits

        *   pplim1 <- pp+ scale_x_discrete(limits = paste("G",seq(5,9),sep="")) + #只显示5-9 scale_y_continuous(limits = c(500, 1500))#只显示小于500-1500的-复杂的写法
        *   pplim2 <- pp + xlim(paste("G",seq(5,9),sep="")) + ylim(500, 1500) #简单的写法

修改4:设置显示的刻度(breaks)

ppbreak <- pp + scale_x_discrete( breaks =  paste("G",seq(5,9),sep=""))+  #只是动了坐标轴全部显示scale_y_continuous(breaks = seq(500,1500, by=100))#500-1500 #每一百设置一个刻度

主题设置
* 局部设置-单次操作一张图
part <- pp + theme_bw()
* 全局设置-操作后面所有的图
theme_set(theme_bw())
* 适合做科研的:theme_bw

标题(名称)-title

图片标题plot.title
* 坐标轴标题(名称)axis.title
* 能修改的文本格式
* 水平对齐方式 hjust 0 0.5 1分别表示左对齐/右对齐/居中
* 垂直对齐方式 vjust 0 0.5 1分别表示上对齐/下对齐/居中
* 字号 size
* 倾斜 face ="italic"

图例的位置

    *   bioinfoplanet <- bioinfoplanet+ theme(legend.position = c(0.95,0.75), #设置水平和垂直位置legend.background = element_rect(fill="white"))#这个调整位置是把图例加入到了图片上不是和默认的一样并列在图片右边

coord_flip(翻转坐标系)
coord_polar(极坐标系)

上一篇 下一篇

猜你喜欢

热点阅读