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(data = dexp_small, aes(x = Sample, y = Expression, color = Gene)) +
geom_point()
3、分组
library(ggplot2)
library(dplyr)
source("pre_data.R")
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")
p<-ggplot(data = dexp, aes(x = Sample, y = Expression))
p + geom_point() +
facet_wrap(~Gene, scales = "free_x", nrow = 5)
dexp_small<-filter(dexp, Gene %in% paste("G", 1:9, sep = ""))
ps<-ggplot(data = dexp_small, aes(x = Sample, y = Expression))
ps + geom_point