memory.limit()
memory.limit(100000)
getwd()
setwd("E:/科学论文/2018Winter_PM25年际变化_SDEI/20190315 文章绘图/Fig_2 重点区域年际变化/")
jpeg("Fig_2_Boxplot_each region_V2.jpeg",width=600,height=600)
p<-ggplot(PM_data,aes(x=region,y=PM,fill=region))+geom_boxplot()+
theme(legend.position="none",axis.text=element_text(size=20),axis.title=element_text(size=20,face="bold")
,plot.title = element_text(size=20,hjust = 0.5))+
labs(title = "(a)箱型图",x=" ",y=expression(PM[2.5]*' ('*mu*"g /"*m^{3}*')'))
#ylab(expression(PM[2.5]*' ('*mu*"g /"*m^{3}*')'))+ggtitle("(a)箱型图")+xlab(" ")
p
dev.off()
getwd()
setwd("E:/科学论文/2018Winter_PM25年际变化_SDEI/20190315 文章绘图/Fig_2 重点区域年际变化/")
jpeg("Fig_2_line chart_V2.jpeg",width=600,height=600)
p<-ggplot(data=PM_data,aes(x=year,y=PM, group=region))+
geom_line(aes(color=region),lwd=2)+
geom_point()+
theme(legend.position="bottom",axis.text=element_text(size=20),axis.title=element_text(size=20,face="bold")
,plot.title = element_text(size=20,hjust = 0.5)
,legend.text = element_text(size = 16, face = 'bold')
,legend.title=element_blank())+
labs(title = "(b)区域年均浓度时间序列",x=" ",y=expression(PM[2.5]*' ('*mu*"g /"*m^{3}*')'))
p
dev.off()
(1)library(patchwork)
R | ggplot拼图 —— patchwork
R小tip(六)ggplot2拼图
(2)这个效果更好
ggplot2|ggpubr进行“paper”组图合并
R 函数学习 - colorRamp() 和 colorRampPalette()
R 语言配色收集
#设置自己想要的colorbar内容
color_bar <- colorRampPalette(c("#ffffff","#c0c0c0", "#ffff80","#d0d000", "#00ff00","#00bb00", "#008000"))
#带n可以设置n个颜色,做梯度
p1<-ggplot() +#一大堆其他内容
scale_fill_gradientn(colors=color_bar(12))+ #带n可以设置n个颜色,做梯度
ggplot2 |legend参数设置,图形精雕细琢
【r<-ggplot2】修改x和y轴刻度
R语言绘图系列:
使用geom_label绘制标签散点图
绘制点,并通过nudge参数对标签进行x轴和y轴上的平移
使用angle参数对标签角度进行设置
geom_label可以使用fill对颜色进行填充,fontface设置字体,geom_text不能填充颜色
parse参数意思是前面传入的是一个数学表达式,size定义标签相对大小。
画一个散点图
annotate函数传入标签
添加矩形
添加短线段
2.1 guide_legend函数(主要参数:color, shape, size)
图例调整函数也属于标度函数的一类,但不可以直接使用加号来连接,必须放在函数中,作为一个参数。
guide_colorbar和guide_legend设置的是不同的图例,guide_colorbar定义色条图例,guide_legend定义普通图例。
2.2 标度函数scale
对于连续型变量,使用的参数是scale_xxx_continous(),对于分类型变量,使用的是scale_xxx_discrete()。
2.3:theme函数
在theme函数中,与图例有关的主要参数有:
标题主要有五种:主标题,副标题,角注,x轴标签和y轴标签
ggtitle()只能定义标题和副标题,默认的位置在左上角。
画上三角矩阵 corrplot(M, type = "upper")
供参考。
corrplot中参数详解
corrplot(corr, method = c("circle", "square", "ellipse", "number", "shade",
"color", "pie"), type = c("full", "lower", "upper"), add = FALSE,
col = NULL, bg = "white", title = "", is.corr = TRUE, diag = TRUE,
outline = FALSE, mar = c(0, 0, 0, 0), addgrid.col = NULL,
addCoef.col = NULL, addCoefasPercent = FALSE, order = c("original",
"AOE", "FPC", "hclust", "alphabet"), hclust.method = c("complete", "ward",
"ward.D", "ward.D2", "single", "average", "mcquitty", "median", "centroid"),
addrect = NULL, rect.col = "black", rect.lwd = 2, tl.pos = NULL,
tl.cex = 1, tl.col = "red", tl.offset = 0.4, tl.srt = 90,
cl.pos = NULL, cl.lim = NULL, cl.length = NULL, cl.cex = 0.8,
cl.ratio = 0.15, cl.align.text = "c", cl.offset = 0.5, number.cex = 1,
number.font = 2, number.digits = NULL, addshade = c("negative",
"positive", "all"), shade.lwd = 1, shade.col = "white", p.mat = NULL,
sig.level = 0.05, insig = c("pch", "p-value", "blank", "n", "label_sig"),
pch = 4, pch.col = "black", pch.cex = 3, plotCI = c("n", "square",
"circle", "rect"), lowCI.mat = NULL, uppCI.mat = NULL, na.label = "?",
na.label.col = "black", win.asp = 1, ...)
是不是有点多,功能实在太强大了,我们挑几个常用的说明。
corr: 用于绘图的矩阵,必须是正方形矩阵,如果是普通的矩阵,需要设置is.corr=FALSE
method: 可视化的方法,默认是圆circle,还有正方形square、椭圆ellipse、数字number、阴影shade、颜色color和饼pie可选。文章开篇处的示例即为饼形,类似月亮周期的大小变化。
type:展示类型,默认全显full,还有下三角lower,或上三角upper可选。
col:颜色设置,可设置颜色起、中、终点颜色。
is.corr:逻辑值,若为TRUE,不强制要求矩阵是正方形的相关系数矩阵,图例范围也会随数据变化
diag:是否显示对角线值。因为对角线全为1,显示只是美观,实际意义不大
tl.cex:名称标签字体大小
tl.col:名称标签字体颜色
cl.lim:值域范围
addrect:设置分组数量,添加矩形
method – 呈现方式,包括circle(默认) – 圆形,square – 方块,ellipse – 椭圆,number – 数字,pie – 饼图,shade – 阴影,color – 颜色;
diag – 是否画对角线,这里设置为不画;
type – 画图的哪一部分,包括full – 全部,lower – 下三角,upper – 上三角;
col – 颜色,默认为由红到蓝,格式为c(bottom, middle, top),bottom – -1处的颜色,middle – 0处的颜色,top – 1处的颜色;这里先用colorRampPalette生成了颜色设置函数col3(number), number – colorbar上分段的数量,这里分了20短,即每0.1为一段;
cl.lim – 颜色的范围,默认为-1到1,注意设置的范围要包含矩阵中的所有数据;
addgrid.col – 网格的颜色,默认为灰色,这里设置为NA,即不画网格;
tl.pos – 坐标轴标签的位置,包括lt – 左边和上边,ld – 左边和对角线,td – 上边和对角线,d – 对角线,n – 不画;这里设置为lt,即行变量名在左边,列变量名在上边;
tl.cex – 标签的大小;
tl.col – 标签的颜色;
tl.srt – 标签旋转的角度;
tl.offset – 标签和图片的相对位置;
hclust.method – 相关系数聚类的方法,默认不聚类,具体方法包括"ward", "ward.D", "ward.D2", "single", "complete", "average", "mcquitty", "median", "centroid",具体读者可自行尝试
outline – 是否画圆圈的边界,默认不画,可为逻辑变量或字符变量(字符变量为设置边界的颜色);
title – 图片的标题。
以上为corrplot()常用的参数,基本上可供读者做出漂亮的相关矩阵图了。