问答社区:http://www.omicsclass.com/ | 组学大讲堂公众号 | 生信课堂 |
课程推荐1:R语言入门与基础绘图 | 课程推荐2:R语言绘图(ggplot) | 所有生信课程:点击 |
一张统计图形是从数据到几何对象图形属性的一个映射。此外,图形中还可能包含数据的统计变换,最后绘制在某个特定的坐标系中,而分面则可以用来生成数据不同子集的图形。
映射(mapping):是把你想要可视化的数据(data)中的一系列变量对应到图形中不同的属性;
几何对象(geom):在图中实际看到的图形元素,如点、线、多边形等;
统计变换(stats):对数据进行的某种汇总,如分组计数、线性回归等。可选,但很有用。
标度(scale):将数据的取值映射到图形空间,如用颜色、大小或形状来表示不同的取值,使读者可以从图形中读取原始数据一些信息。展现方式为绘制图例和坐标轴。
坐标系(coord):描述数据是如何映射到图形所在的平面,同时提供看图所需的坐标轴和网络线。通常使用笛卡尔坐标系,但也可以变换为其他类型,如极坐标和地图投影。
分面(facet):描述如何将数据分解为各个子集,以及如何对子集作图并联合进行展示。分面也称为条件作图或网格作图。
ggplot2官方帮助文档:https://ggplot2.tidyverse.org/reference/index.html
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
dim(diamonds) #示例数据
## [1] 53940 10
#随机选取1000行数据,减少数据量加快绘图时间
mydiamonds<-sample_n(diamonds,1000)
ggplot中“+” 运算符,可以多次使用,添加不同的图层。
#点图
ggplot(data=mydiamonds,mapping=aes(x=carat,y=price))+geom_point()
# 增加点的颜色映射
ggplot(data=mydiamonds,aes(x=carat,y=price,color=color))+geom_point()
ggplot(data=mydiamonds,aes(x=carat,y=price,color=color,shape=cut))+geom_point()
## Warning: Using shapes for an ordinal variable is not advised
#box图
ggplot(data=mydiamonds,aes(x=color,y=price))+geom_boxplot()
#柱状图
ggplot(data=mydiamonds,aes(x=color))+geom_bar()
#增加颜色映射
ggplot(data=mydiamonds,aes(x=color,fill=clarity))+geom_bar()
#频率直方图
ggplot(data=mydiamonds,aes(x=price))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data=mydiamonds,aes(x=price))+geom_histogram(binwidth = 1000)
ggplot(data=mydiamonds,aes(x=price,fill=color))+geom_histogram(binwidth = 1000)
#密度分布图
ggplot(data=mydiamonds,aes(x=price))+geom_density()
ggplot(data=mydiamonds,aes(x=price,color=color))+geom_density()
ggplot中“+” 运算符,可以多次使用,添加不同的图层。
p<-ggplot(data=mydiamonds,aes(x=carat,y=price))+geom_point()
print(p)
p<-ggplot(data=mydiamonds,aes(x=carat,y=price))+geom_point()+geom_smooth()
print(p)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
p<-ggplot(data=mydiamonds,aes(x=carat,y=price))+geom_point()+geom_line()
print(p)
p<-ggplot(data=mydiamonds,aes(x=carat,y=price,color=cut))+geom_point()
print(p)
p1<-p+ xlab("Carat")+ylab("Price")+ggtitle("mydiamonds")
print(p1)
#或者
p2<-p+ labs(x="Carat",y="Price",title="mydiamonds",color="my cut")
print(p2)
颜色标度修改,color代表点线的设置,fill填充颜色设置
以下为修改颜色的方法
scale_color_hue()
scale_fill_hue()
scale_color_brewer()
scale_fill_brewer()
scale_color_manual() 手动设置颜色
scale_fill_manual() 手动设置颜色
#修改配色方案
#柱状图用fill设置颜色映射
ggplot(data=mydiamonds,aes(x=price,fill=color))+geom_histogram(binwidth = 1000)
#修改一下配色
ggplot(data=mydiamonds,aes(x=price,fill=color))+geom_histogram(binwidth = 1000)+
scale_fill_hue()
#借用一下第三方配色方案 RColorbrewer
ggplot(data=mydiamonds,aes(x=price,fill=color))+geom_histogram(binwidth = 1000)+
scale_fill_brewer(palette = "Set1")
#DIY 设置自己喜欢的颜色,并且指定分组各自的颜色
mycolor<-c(F="#E41A1C", D="#377EB8",E= "#4DAF4A" ,I="#984EA3" ,H="#FF7F00" ,J="#FFFF33" ,G="#A65628")
ggplot(data=mydiamonds,aes(x=price,fill=color))+geom_histogram(binwidth = 1000)+
scale_fill_manual(values=mycolor)
####同样的道理对于点的颜色设置,使用color:
ggplot(data=mydiamonds, aes(carat, price)) + geom_point(aes(colour = clarity))
#修改一下配色,注意 用的scale_color_hue(),大家也可以用用一下scale_fill_hue() 看看有什么效果?
ggplot(data=mydiamonds, aes(carat, price)) + geom_point(aes(colour = clarity))+scale_color_hue()
#借用一下第三方配色方案 RColorbrewer
ggplot(data=mydiamonds, aes(carat, price)) + geom_point(aes(colour = clarity))+scale_color_brewer(palette = "Set1")
#自己设置喜欢的颜色
ggplot(data=mydiamonds,aes(carat, price,color=color))+geom_point()+
scale_color_manual(values=c("#66C2A5", "#FC8D62", "#8DA0CB", "#E78AC3" ,"#A6D854", "#FFD92F","#E5C494" ,"#B3B3B3"))
#aes中映射shape
ggplot(data=mydiamonds,aes(x=carat,y=price,shape=cut,color=cut))+geom_point()+
xlab("Carat")+ylab("Price")+ggtitle("mydiamonds")
## Warning: Using shapes for an ordinal variable is not advised
#修改shape
ggplot(data=mydiamonds,aes(x=carat,y=price,shape=cut,color=cut))+geom_point()+
xlab("Carat")+ylab("Price")+ggtitle("mydiamonds")+
scale_shape_manual(values = c(1,2,6,0,23))
#坐标轴label修改,非连续型坐标轴:scale_x_discrete(),scale_y_discrete()
p<-ggplot(mydiamonds, aes(cut)) + geom_bar()
print(p)
p+scale_x_discrete("Cut", labels = c("Fair" = "F","Good" = "G","Very Good" = "VG","Perfect" = "P","Ideal" = "I"))
#坐标轴label修改,连续型坐标:scale_x_continuous(),scale_y_continuous()
p<-ggplot(data=mydiamonds,aes(x=carat,y=price))+geom_point()
print(p)
p+scale_x_continuous("Carat") +scale_y_continuous("Price") #修改坐标轴标题
p + scale_x_continuous(limits = c(0, 2))
## Warning: Removed 37 rows containing missing values or values outside the scale range
## (`geom_point()`).
p+scale_x_continuous(breaks = c(1,3),label = c( "one","three"))
例如:图片的title主题元素名称为plot.title,类型是文字,用element_text()方法修改显示样式,如字号调大一下:
p1<-ggplot(mtcars, aes(wt, mpg)) +geom_point() +labs(title="Fuel economy declines as weight increases")
p1
p1 + theme(plot.title=element_text(size=rel(2)))
类型 | 修改方法 |
---|---|
line,segment | element_line() |
rect | element_rect() |
text | element_text() |
另外:还有一个方法 element_blank() 表示什么都不画,清除。
更多主题修改:https://ggplot2.tidyverse.org/reference/theme.html
方法中的选项意义:
ggplot(data=mydiamonds,aes(x=price))+geom_histogram(binwidth = 1000)+
theme(axis.text.x=element_text(color="red",angle=45),
axis.text.y=element_text(color="red"),
axis.title.x =element_text(color="blue",face="bold"),
axis.title.y=element_text(color="blue",face="bold",angle = 90)
)
方法中的一些选项意义:
ggplot(data=mydiamonds,aes(x=price))+geom_histogram(binwidth = 1000)
p<-ggplot(data=mydiamonds,aes(x=price))+geom_histogram(binwidth = 1000)
print(p)
p+theme(plot.background = element_rect(fill="red"),
panel.background = element_rect(fill="blue"))
4.1.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1
#线修改:element_line() ###################
p<-ggplot(data=mydiamonds,aes(x=price))+geom_histogram(binwidth = 1000)
p+theme(panel.grid.major = element_line(color="red"),
panel.grid.minor = element_line(color="blue",linetype = "dashed"),
panel.background = element_blank()
)
p+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank()
)
p+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(color="red",linewidth=1)
)
#利用:panel.background 设置绘图边框
p+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill=NA,color = "red",linewidth=2),
axis.line = element_line(color="red",size=1)
)
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p<-ggplot(data=mydiamonds,aes(x=carat,y=price,shape=cut,color=cut))+geom_point()+
xlab("Carat")+ylab("Price")+ggtitle("mydiamonds")+
scale_shape_manual(values = c(1,2,6,0,23))
print(p)
#修改legend
#可以直接删除
p+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill=NA,color = "red",linewidth=2),
legend.position='none'
)
p+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill=NA,color = "red",linewidth=2),
legend.key = element_blank(),
legend.title = element_blank()
)
p<-ggplot(data=mydiamonds,aes(x=carat,y=price,color=cut))+geom_point()+
xlab("Carat")+ylab("Price")+ggtitle("mydiamonds")+scale_color_brewer(palette = "Set1")
p+theme_bw()
p+theme_linedraw()
p+theme_light()
p+theme_dark()
p+theme_classic()
#使用绘图设备输出
pdf(file="out.pdf",width = 6,height = 6)
print(p)
dev.off()
png(filename = "out.png",width = 5,height = 5,units = "in",dpi=300)
print(p)
dev.off()
#也可以用ggplot自带的方法输出图片
ggsave(filename = "out1.png",width = 6,height = 6,units = "in",dpi=300)
ggsave(filename = "out1.pdf",width = 6,height = 6)
sessionInfo()
## R version 4.2.2 (2022-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Rocky Linux 9.2 (Blue Onyx)
##
## Matrix products: default
## BLAS: /share/biosoft/R/R-v4.2.2/lib64/R/lib/libRblas.so
## LAPACK: /share/biosoft/R/R-v4.2.2/lib64/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] dplyr_1.1.4 ggplot2_3.5.0.9000
##
## loaded via a namespace (and not attached):
## [1] RColorBrewer_1.1-3 highr_0.10 bslib_0.6.1 compiler_4.2.2
## [5] pillar_1.9.0 jquerylib_0.1.4 tools_4.2.2 digest_0.6.33
## [9] lattice_0.22-5 nlme_3.1-164 viridisLite_0.4.2 jsonlite_1.8.8
## [13] evaluate_0.23 lifecycle_1.0.4 tibble_3.2.1 gtable_0.3.4
## [17] mgcv_1.9-1 pkgconfig_2.0.3 rlang_1.1.2 Matrix_1.6-5
## [21] cli_3.6.2 rstudioapi_0.15.0 yaml_2.3.8 xfun_0.41
## [25] fastmap_1.1.1 withr_2.5.2 knitr_1.45 generics_0.1.3
## [29] vctrs_0.6.5 sass_0.4.8 grid_4.2.2 tidyselect_1.2.0
## [33] glue_1.6.2 R6_2.5.1 fansi_1.0.6 rmarkdown_2.25
## [37] farver_2.1.1 magrittr_2.0.3 splines_4.2.2 scales_1.3.0
## [41] htmltools_0.5.7 colorspace_2.1-0 labeling_0.4.3 utf8_1.2.4
## [45] munsell_0.5.0 cachem_1.0.8 crayon_1.5.2