小组多图-ggplot2

本科室友拖我画的,要表示不同年份,不同地区的温度,降水量等指标的差异。我第一反应用tableau画,但是毕竟成长了,要用R来锻炼一下。



她说老师要申本子用,白纸黑字,画彩色的也没用,所以就出了个黑白的图。
代码如下,给大家参考

options(stringsAsFactors = F)
library(readxl)
df<-read_excel("qx.xlsx",sheet=1)
df<-as.data.frame(df)
library(ggplot2)
library(cowplot)
pT<-ggplot(df,aes(x=Year,y=T))+geom_point(size=0.5)+facet_wrap(~Site,nrow=1)+xlab("")+theme(axis.text.x=element_blank(),axis.ticks.x = element_blank())+scale_y_continuous(breaks =seq(2,14,by=4))+stat_smooth(method=lm)
pwater<-ggplot(df,aes(x=Year,y=P))+geom_point(size=0.5)+facet_wrap(~Site,nrow=1)+xlab("")+theme(axis.text.x=element_blank(),axis.ticks.x = element_blank(),legend.title=element_blank())+scale_y_continuous(breaks =seq(260,1500,by=400))+stat_smooth(method=lm)
pTmin<-ggplot(df,aes(x=Year,y=Tmin))+geom_point(size=0.5)+facet_wrap(~Site,nrow=1)+xlab("")+theme(axis.text.x=element_blank(),axis.ticks.x = element_blank(),legend.title=element_blank())+scale_y_continuous(breaks =seq(-40,-5,by=10))+stat_smooth(method=lm)
pTmax<-ggplot(df,aes(x=Year,y=Tmax))+geom_point(size=0.5)+facet_wrap(~Site,nrow=1)+theme(legend.title=element_blank())+scale_x_continuous(breaks =seq(1951,2013,by=10))+theme(axis.text.x = element_text(angle = 40, hjust = .5, vjust = .5,size = 8))+scale_y_continuous(breaks =seq(30,45,by=5))+stat_smooth(method=lm)
plot_grid(pT,pwater,pTmin,pTmax,ncol = 1)

思路是先画四个横排图,最后用cowplot这个包给合并一下。
用到的核心语法如下,替大家画重点了

控制X轴或者Y轴的刻度
scale_y_continuous(breaks =seq(2,14,by=4))
添加趋势线
stat_smooth(method=lm)
分面展示
facet_wrap(~pthway,nrow=2)
去掉X轴坐标
theme(axis.text.x=element_blank())
去掉X轴刻度尺
theme(axis.ticks.x = element_blank())
去掉X轴标题
theme(axis.title.x = element_blank())
坐标倾斜
theme(axis.text.x = element_text(angle = 40, hjust = .5, vjust = .5,size = 8))

你可能感兴趣的:(小组多图-ggplot2)