title: "class6.1"
author: "xiaowang"
date: "2023-04-18"
output: html_document
与绘图相关的R包的安装
options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
#R包的安装
if(!require(ggplot2))install.packages('ggplot2',update = F,ask = F)
if(!require(ggpubr))install.packages('ggpubr',update = F,ask = F)
if(!require(eoffice))install.packages("eoffice",update = F,ask = F)
if(!require(patchwork))install.packages("patchwork",update = F,ask = F)
#检查是否安装成功
library(ggplot2)
library(ggpubr)
library(eoffice)
library(patchwork)
_plots.R
#1.基础包
plot(iris[,1],iris[,3],col=iris[,5])
text(6.5,4,labels="cfr")
#关闭画板
dev.off()
## null device
## 1
#2.ggplot2
#ggplot(data=<DATA>)+
#<GEOM_FUNCTION(mapping=aes(<MAPPINGS>))
library(ggplot2)
ggplot(data = iris)+
geom_point(mapping = aes(x=Sepal.Length,
y=Petal.Length,
color=Species))
#手动设置
ggplot(data = iris)+
geom_point(mapping = aes(x=Sepal.Length,y=Petal.Length),color="blue")
ggplot(data = iris)+
geom_point(mapping = aes(x=Sepal.Length,y=Petal.Length),
size=5, #点的大小5mm
alpha=0.5, #透明度 50%
shape=8) #点的形状
ggplot(data=iris)+
geom_point(mapping = aes(x=Sepal.Length,
y=Petal.Length,
color=Species))+
scale_color_manual(values = c("blue","grey","red"))
#color和fill两个属性
ggplot(data=iris)+
geom_point(mapping = aes(x=Sepal.Length,
y=Petal.Length,
color=Species),
shape=17) #17号,实心的例子
ggplot(data = iris)+
geom_point(mapping = aes(x=Sepal.Length,
y=Petal.Length,
color=Species),
shape=2) #2号,空心的例子
ggplot(data=iris)+
geom_point(mapping = aes(x=Sepal.Length,
y=Petal.Length,
color=Species),
shape=24,
fill="black") #双色的例子
#分面
ggplot(data = iris)+
geom_point(mapping = aes(x=Sepal.Length,y=Petal.Length))+
facet_wrap(~Species)
#双分面
dat=iris
dat$Group=sample(letters[1:5],150,replace = T)
ggplot(data = dat)+
geom_point(mapping = aes(x=Sepal.Length,y=Petal.Length))+
facet_grid(Group~Species)
#几何对象
ggplot(data=iris)+
geom_smooth(mapping = aes(x=Sepal.Length,y=Petal.Length))+
geom_point(mapping = aes(x=Sepal.Length,y=Petal.Length))
ggplot(data = iris,mapping = aes(x=Sepal.Length,y=Petal.Length))+
geom_smooth()+
geom_point()
#统计变换
ggplot(data = diamonds)+
geom_bar(mapping = aes(x=cut))
ggplot(data = diamonds)+
stat_count(mapping = aes(x=cut))
ggplot(data = diamonds)+ #不统计count,统计prop(比例)
geom_bar(mapping = aes(x=cut,y=..prop..,group=1) )
#位置关系
ggplot(data = iris,mapping = aes(x=Species,y=Sepal.Width))+
geom_boxplot(aes(fill=Species))+
geom_jitter()
#完美模板
#ggplot(data=<DATA>)+
# <GEOM_FUNCTION>(
# mapping=aes(<MAPPINGS>),
# stat=<STAT>,
# position=<POSITION>)+
# <COORDINATE_FUNCTION>+
# <FACET_FUNCTION>
#3._ggpubr.R
library(ggpubr)
ggscatter(iris,x="Sepal.Length",y="Petal.Length",color = "Species")
p<-ggboxplot(iris,x="Species",y="Sepal.Length",
color = "Species",shape="Species",
add = "jitter")
my_comparisons<-list(c("setosa","versicolor"),
c("setosa","virginica"),
c("versicolor","virginica"))
p+stat_compare_means(comparisons = my_comparisons)+
stat_compare_means(label.y = 9)
#图片保存
ggsave("iris_box_ggpubr.png")
ggsave(p,filename="iris_box_ggpubr2.png")
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")
以上内容来自生信技能树
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。