ggplot2笔记

这是一篇关于ggplot2的笔记,涵盖了数据准备、映射、分组、分面、几何对象与统计变换的关系,以及基本图形类型的创建,适合R语言用户学习使用。
摘要由CSDN通过智能技术生成

ggplot笔记


title: “ggplot学习”

author: “ranshan”

date: ‘2022-05-23’


1、数据准备


#准备数据 

#设置工作目录 
setwd("D://Rworkspace")

#读取数据
gene_exp<-read.table(file = "gene_exp.txt",
                     sep = "\t",
                     header = T,
                     stringsAsFactors = F)
gene_len<-read.table(file = "gene_len.txt",
                     sep = "\t",
                     header = T,
                     stringsAsFactors = F)
group<-read.table(file = "group.txt",
                  sep = "\t",
                  header = T,
                  stringsAsFactors = F)

#数据变形
library(tidyr)
library(dplyr)
dexp<-gather(data = gene_exp, 
             key = Sample, 
             value = Expression, 
             -Gene) %>% 
  left_join(gene_len, by = "Gene") %>%
  left_join(group, by = "Sample") %>%
  select(Gene, Sample, Group, Expression, Length) %>%
  arrange(Gene)

2、映射

#快速入门
library(ggplot2)
library(dplyr)

#准备数据 
dexp_small<-filter(dexp, Group == "group1", Gene %in% paste("G", 1:10, sep = "" )) %>%
  select(-Group, -Length)

#ggplot:映射 几何对象 图层 
ggplot(data = dexp_small, aes(x = Sample, y = Expression, color = Gene)) +
  geom_point() 

3、分组

library(ggplot2)
library(dplyr)

#准备数据 
source("pre_data.R")

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


p<-ggplot(data = dexp, aes(x = Sample, y = Expression)) 

p + geom_point(aes(color = Gene,
                 size = Length,
                 shape = Group),
             alpha = 8/10) 

##分组 
p + geom_boxplot(aes(group = Group))

p + geom_line(aes(group = Gene, color = Gene)) +
  geom_smooth(aes(group = 1))

4、分面

##分面 
library(ggplot2)
library(dplyr)

#准备数据 
source("pre_data.R")

##分面
#facet_wrap(facets, nrow = NULL, ncol = NULL, scales = "fixed",
#           shrink = TRUE, labeller = "label_value", as.table = TRUE,
#           switch = NULL, drop = TRUE, dir = "h", strip.position = "top")
#重要参数:
#facets:  分面参数如 ~Group,表示用 Group 变量进行数据分类
#nrow:    绘制图形的行数
#ncol:    绘制图形的列数,一般nrow/ncol只设定一个即可
#scales: fixed,小图均使用统一坐标;
#         free每个小图按照各自数据范围自由调整坐标;
#         free_x为自由调整x轴刻度范围;
#         free_y为自由调整y轴刻度范围。
p<-ggplot(data = dexp, aes(x = Sample, y = Expression)) 
p + geom_point() +
  facet_wrap(~Gene, scales = "free_x", nrow = 5)


#facet_grid(facets, margins = FALSE, scales = "fixed", space = "fixed",
#           shrink = TRUE, labeller = "label_value", as.table = TRUE,
#           switch = NULL, drop = TRUE)
#与facet_wrap不同的重要参数:
#facets:  应用两个标准分面,如Gene ~ Group
#margins: Ture,包含所有数据的组 
#space:   每张小图的坐标宽度,值同scales
dexp_small<-filter(dexp, Gene %in% paste("G", 1:9, sep = ""))
ps<-ggplot(data = dexp_small, aes(x = Sample, y = Expression)) 
ps + geom_point
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值