1. 程式人生 > 其它 >R繪圖(02)——ggplot簡單折線圖

R繪圖(02)——ggplot簡單折線圖

資料庫依然採用的01篇的資料庫

資料處理思路和01篇異曲同工之妙

這次是根據年份繪製平均氣溫,即需要先求得各個時間點的平均氣溫,再根據xy進行繪製

 

效果如下:

 

 

 

var2<-c("Year_F","TempC")
lineICE<-ICE[var2]
colnames(lineICE) <- c("YEAR","T")
mean_C <- matrix(NA, nrow=8, ncol=2)
for (i in 1:8){
  mean_C[i,1] <- i+2010
  mean_C[i,2] <- mean(lineICE$T[lineICE$YEAR==i+2010
],na.rm = TRUE) } mean_C<-as.data.frame(mean_C) colnames(mean_C) <- c("YEAR","T") lineACL<-ACL[var2] colnames(lineACL) <- c("YEAR","T") mean_A <- matrix(NA, nrow=3, ncol=2) for (i in 1:3){ mean_A[i,1] <- i+2016 mean_A[i,2] <- mean(lineACL$T[lineACL$YEAR==i+2016],na.rm = TRUE) } mean_A
<-as.data.frame(mean_A) colnames(mean_A) <- c("YEAR","T") ICET<-ggplot(data = mean_C, mapping = aes(x = YEAR, y = T)) + geom_line()+ theme_bw() + theme(panel.grid=element_blank()) ICET<-ICET+scale_x_continuous(breaks = seq(2011, 2018, by = 1))+ylab("Temperature") ACLT<-ggplot(data = mean_A, mapping = aes(x = YEAR, y = T)) + geom_line()+ theme_bw() + theme(panel.grid=element_blank()) ACLT
<-ACLT+scale_x_continuous(breaks = seq(2017, 2019, by = 1))+ylab("Temperature") ggarrange(ICET,ACLT, labels = c("A", "B"),ncol = 1, nrow = 2)