본문 바로가기

R 프로그래밍

R 응용 - 표층수온분포도 및 수직수온분포도 그리기

 

정선해양관측자료를 이용하여 다음 조건의 표층수온분포도, 수직수온분포도 그리기
  • 기간 : 2011년~2021년
  • 2월과 8월 평균수온 분포도 
  • 표층수온 : 동해구역 , 수직수온 : 105번라인

관측자료 출처 : https://www.nifs.go.kr/kodc/soo_list.kodc

 

 

 

# 2011~2021년 동해 구역 data 추출

 

1)  관측data(csv 파일) 불러오기

```{r}
dat<-read.csv(file="C:\\r_workspace\\data\\jungsun.csv", header=T)
head(dat)

```

 

 

2) 동해, 2010년~2021년 data만 추출

dplyr 패키지 설치  (install.packages("dplyr")

```{r}
library(dplyr)            

dd <-dat%>%filter( yr>2010, sea=="동해")
head(dd)             
```

 

 

# 2월, 8월 표층수온분포도 그리기 (depth = 0)

 

3)  2월과 8월 data만 추출하여 새로운 data 생성

```{r}
east2 <-dd %>%filter(depth=="0", mon=="2")%>%arrange(yr, mon, ls_name)

east2
east8<- dd %>%filter(depth=="0", mon=="8")%>%arrange(yr, mon, ls_name)
east8

 

4)  관측정점 위치 확인

```{r}
library(leaflet);library(mapview);library(leafsync)
m1<-leaflet(east2) %>% addProviderTiles(providers$Esri.WorldStreetMap)%>%addCircles(~lon,~lat, label=~ls_name, weight = 5,color="blue", labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, style=list("color"="gray", "font-size" = "15px","font-family" = "serif" )))
m1

m2<-leaflet(east8) %>% addProviderTiles(providers$Esri.WorldStreetMap)%>%addCircles(~lon,~lat, label=~ls_name, weight = 5,color="red", labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, style=list("color"="gray", "font-size" = "15px","font-family" = "serif" )))
m2

latticeView(m1,m2, ncol=2)
```

 

 

5)  2월, 8월 평균수온산출하고 (파일 저장)

```{r}
avg2 <-east2%>%group_by(ls_name)%>%summarise(avg_temp=mean(temp,na.rm=T),
 lat=mean(lat),lon=mean(lon))
avg8 <-east8%>%group_by(ls_name)%>%summarise(avg_temp=mean(temp,na.rm=T),               lat=mean(lat),lon=mean(lon))
avg2
avg8

write.csv(avg2,"C:\\r_workspace\\data\\jungsun_avg2.csv") write.csv(avg8,"C:\\r_workspace\\data\\jungsun_avg8.csv")
```

 

6)  2월 8월 평균수온 분포도 그리기

```{r}
library(akima); library(ggplot2);library(dplyr);library(tidyr);library(metR)

fld2<-interp2xyz(interp(x=avg2$lon, y=avg2$lat, z=avg2$avg_temp, duplicate="mean"),     
          data.frame=TRUE)%>% filter(!is.na(z)) %>% tbl_df()
ggplot(data = fld2 , aes(x = x, y = y, z=z, fill = z)) + geom_raster() + scale_fill_distiller("평균수온", palette="Spectral", na.value="white",limits = c(7,14)) + geom_contour(color="black")+ geom_text_contour(stroke = 0.15)+theme_bw()+ xlab("longtitude")+ylab("latitude")+ ggtitle("2011년~2021년 2월 표층수온분포") + theme(plot.title = element_text(hjust = 0.5))

fld8<-interp2xyz(interp(x=avg8$lon, y=avg8$lat, z=avg8$avg_temp, duplicate="mean"),
           data.frame=TRUE)%>% filter(!is.na(z)) %>% tbl_df()
ggplot(data = fld8 , aes(x = x, y = y, z=z, fill = z)) + geom_raster() + scale_fill_distiller("평균수온", palette="Spectral", na.value="white",limits = c(23,28)) + geom_contour(color="black")+ geom_text_contour(stroke = 0.15)+theme_bw()+ xlab("longtitude")+ylab("latitude")+ ggtitle("2011년~2021년 8월 표층수온분포") + theme(plot.title = element_text(hjust = 0.5))
```

 

 

 

# 2월, 8월 105번 라인 수직수온분포도 그리기 (l_name=105)

 

7)  105번, 2011~2021년 2월, 8월 데이터 추출

```{r}
v105_2 <-dd %>%filter(l_name=="105", mon=="2", depth%in%c(10,20,30,50,75,125,150,200,250,300,400,500))%>%arrange(yr, mon, ls_name)
v105_2

v105_8 <-dd %>%filter(l_name=="105", mon=="8", depth%in%c(10,20,30,50,75,125,150,200,250,300,400,500))%>%arrange(yr, mon, ls_name)
v105_8
```

 

 

8)  105번 수심대별 평균수온 구하기

```{r}
temp2<-v105_2%>%group_by(ls_name,depth)%>% summarise(avg_temp=mean(temp, na.rm=T), lon=mean(lon), lat=mean(lat))%>% arrange(ls_name,depth)
temp2
temp8<-v105_8%>%group_by(ls_name, depth)%>% summarise(avg_temp=mean(temp,na.rm=T), lon=mean(lon),lat=mean(lat) )%>% arrange(ls_name,depth)
temp8
```

 

 

9)  보간하고 수직수온분포도 그리기

```{r}
library(gridExtra);library(ggplot2); library(metR)
min(temp2$avg_temp)
max(temp2$avg_temp)
f2<-interp2xyz(interp(x=temp2$lon, y=temp2$depth, z=temp2$avg_temp, duplicate="mean"), data.frame=TRUE)%>% filter(!is.na(z)) %>% tbl_df()
ggplot(data = f2 , aes(x = x, y = y, z=z, fill = z)) + geom_raster() + scale_fill_distiller("평균수온", palette="Spectral", na.value="white",limits = c(0,14)) + geom_contour(color="black",binwidth=1)+
geom_text_contour(stroke = 0.15)+theme_bw()+ scale_x_continuous("Longtitude",breaks=seq(128,133, by=0.5))+ scale_y_reverse("Depth(m)")+ ggtitle("2011년~2021년 2월 연직수온분포") + theme(plot.title = element_text(hjust = 0.5))

min(temp8$avg_temp)
max(temp8$avg_temp)
f8<-interp2xyz(interp(x=temp8$lon, y=temp8$depth, z=temp8$avg_temp, duplicate="mean"), data.frame=TRUE)%>% filter(!is.na(z)) %>% tbl_df()
ggplot(data = f8 , aes(x = x, y = y, z=z, fill = z)) + geom_raster() + scale_fill_distiller("평균수온", palette="Spectral", na.value="white",limits = c(0,26)) + geom_contour(color="black",binwidth=2)+ geom_text_contour(stroke = 0.15)+theme_bw()+ scale_x_continuous("Longtitude",breaks=seq(128,133, by=0.5))+ scale_y_reverse("Depth(m)")+ ggtitle("2011년~2021년 8월 연직수온분포") + theme(plot.title = element_text(hjust = 0.5))
```