本科室友拖我畫的,要表示不同年份,不同地區(qū)的溫度,降水量等指標(biāo)的差異。我第一反應(yīng)用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軸坐標(biāo)
theme(axis.text.x=element_blank())
去掉X軸刻度尺
theme(axis.ticks.x = element_blank())
去掉X軸標(biāo)題
theme(axis.title.x = element_blank())
坐標(biāo)傾斜
theme(axis.text.x = element_text(angle = 40, hjust = .5, vjust = .5,size = 8))