title: "note4"
output: html_document
date: "2023-05-16"
#设置镜像
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)
#作图分三类
#1.基础包 略显陈旧 了解一下
plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello')#在6.5,4坐标处加上hello
dev.off() #关闭画板
#2.ggplot2 中坚力量,语法有个性,theme_bw()+#把灰色主题背景去掉,theme_classic()#只留下横纵坐标
library(ggplot2)
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
#3.ggpubr 函数没有ggplot2多,但图会好看点,没有灰色背景,只留横纵坐标,不能满足所有需求,新手友好型 ggplot2简化和美化 褒贬不一
library(ggpubr)
ggscatter(iris,
x="Sepal.Length",
y="Petal.Length",
color="Species")
#ggplot2语法1入门级绘图模板,2映射,3分面,4几何对象,5统计变换,6位置调整,7坐标系
library(ggplot2)
#1.入门级绘图模板:作图数据,横纵坐标
#基础语法 行末写+,缩进,列名不需要""
#ggplot(data=<DATA>)+
# <GEOM_FUNCTION>(mappings=aes(<MAPPINGS>))
#如iris data中以列名Sepal和Petal.Length为横纵坐标画点状图
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#2.属性设置(颜色color、大小size、透明度alpha、点的形状shape,线型,填充颜色fill等)
#2.1 手动设置,需要设置为有意义的值
#手动设置颜色和点的参数,手动设置需要设为有意义的值。颜色:字符串,blue,red等大小:单位mm形状:数字编号表示
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length),
color = "blue") #color是geom_point的参数
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length),
size = 5, # 点的大小5mm
alpha = 0.5, # 透明度 50%
shape= 8) #点的形状,数字编号表示,一共20个数字编号
#2.2 映射:按照数据框的某一列来定义图的某个属性
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))#color为要映射的分组变量,按照花的品种来分配颜色,默认红蓝绿,此时color是aes的参数
## Q1 能不能自行指定映射的具体颜色?
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+
scale_color_manual(values = c("blue","grey","red"))#scale_color_manual和color = Species对应
## Q2 区分color和fill两个属性
### Q2-1 空心形状和实心形状都用color设置颜色
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号,空心的例子
### Q2-2 既有边框又有内心的,才需要color外边框和fill内实心(条形图和箱线图等,fill = Species)两个参数
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 24,
fill = "black") #24号,双色的例子
#3.分面
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species) #按照Species分成3个面
#双分面
dat = iris#不要修改内置数据,因为会忘记改过,此时重置R可以恢复,但最好还是赋值
dat$Group = sample(letters[1:5],150,replace = T)##新增一列Group,replace=T时,可以取超过5的数,abcde中放回取样150次,replace默认是F,不能取超过当前元素的值
ggplot(data = dat) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_grid(Group ~ Species)
#facet_grid根据Group和Species两个变量进行分组,并用网格的方式展示每个组合的数据。其中每一行代表一个Group,每一列代表一种Species。
#4.几何对象
#局部设置和全局设置
ggplot(data = iris) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+#局部设置,仅对当前图层有效
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#两个图用两个函数+,不想代码写两次,可以在全局写映射,把mapping=aes写进ggplot里,全局设置
ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+#全局设置,对所有图层有效
geom_smooth()+#画出的单个图层,也称几何对象
geom_point()
#如在箱线图中叠加点图
ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width,fill = Species))+
geom_boxplot()+
geom_point()#代码不报错不代表结果没错,应该有50个点,但图上展示只有十几个点,应加个x轴抖动+geom_jitter()函数
#5.统计变换-柱状图
View(diamonds)#diamonds内置数据
table(diamonds$cut)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))#纵坐标不用写,自动统计计算y,几何函数
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))#统计变量函数
#统计变换使用场景
#5.1.不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")#指定x,y作图,此时不会自动统计计算y
#5.2count改为prop
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))#将自动统计计算ycount改成prop
#6.位置关系
# 6.1抖动的点图
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_boxplot()+
geom_point()
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_boxplot()+
geom_jitter()
# 6.2堆叠直方图,堆叠和并列数据没有区别,只是展示方式不同
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
# 6.3 并列直方图,position = "dodge"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
#7.坐标系
#翻转坐标系coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
#翻转极坐标系coord_polar()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar
bar + coord_flip()
bar + coord_polar()
#复杂作图
data=iris
ggplot(data, aes(x = Sepal.Width, y = Species)) +
geom_violin(aes(fill = Species))+#小提琴图,这里在映射,所以不能直接写fill = Species,要写映射函数mapping = aes()
geom_boxplot()+
geom_jitter(aes(shape = Species))+#加抖动,形状映射shape =
theme_bw()+#把灰色主题背景去掉
theme_classic()#只留下横纵坐标
# ggpubr 搜代码直接用,基本不需要系统学习
# sthda上有大量ggpubr出的图
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")
p
my_comparisons <- list( c("setosa", "versicolor"),
c("setosa", "virginica"),
c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons#组间比较Add pairwise comparisons p-value
stat_compare_means(label.y = 9)
#图片保存的三种方法,后缀需要正确,不像文件对后缀没有要求
#1.基础包作图的保存,通用三段论
pdf("iris_box_ggpubr.pdf")#保存的格式及文件名
boxplot(iris[,1]~iris[,5])#作图代码
text(6.5,4, labels = 'hello')
dev.off()#画完了,关闭画板,经常出现代码可运行但不出图,因为画板被占用,此时多次运行dev.off(),到null device为止,再重新运行出图代码或dev.new()
#2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave .png
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")#给图片命名
#3.eoffice包 导出为ppt,全部元素都是可编辑模式,ppt打开右键组合取消组合,可编辑字体,颜色,字号等等
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")#后缀是.pptx
#拼图https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ
#画图合辑https://www.yuque.com/xiaojiewanglezenmofenshen/dbwkg1/rhgmyb
#直接找画图代码,sthda网站或者直接搜
#ggplot2中的labs函数可以修改图的标题,横纵轴名称等,https://www.cnblogs.com/xudongliang/p/7057974.html
#设置图片的标题(title), 子标题(subtitle), 引用(caption)
ggplot(mtcars, aes(mpg, wt, colour = cyl)) +
geom_point() +
labs(title = "This is title",
subtitle = "This is subtitle",
caption = "This is caption")
#修改图片的x轴和y轴的标题
ggplot(mtcars, aes(mpg, wt, colour = cyl)) +
geom_point() +
xlab(label='new x')+
ylab(label='new y')
#修改图例的标题
ggplot(mtcars, aes(mpg, wt, colour = cyl)) +
geom_point() +
labs(colour = "New Cyl")
## Error: <text>:214:3: unexpected symbol
## 213: p + stat_compare_means(comparisons = my_comparisons#组间比较Add pairwise comparisons p-value
## 214: stat_compare_means
## ^
引用自生信技能书,闻季起舞 This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。