小組多圖-ggplot2

本科室友拖我畫的,要表示不同年份,不同地區(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))

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容