【R】ggplot2绘图技巧

0. 基本内容介绍

ggplot2|详解八大基本绘图要素


1. boxplot例子

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()



2. line chart例子

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()


3. ggplot2拼图

(1)library(patchwork)

R | ggplot拼图 —— patchwork

R小tip(六)ggplot2拼图


(2)这个效果更好

ggplot2|ggpubr进行“paper”组图合并


4. 调colorbar

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个颜色,做梯度


5. legend设置

ggplot2 |legend参数设置,图形精雕细琢


6. 修改x轴和y轴刻度

【r<-ggplot2】修改x和y轴刻度

你可能感兴趣的:(【R】ggplot2绘图技巧)