본문 바로가기
연륜연대학/R Coding

[ggplot2] 간트차트 만들기(연대기차트) Gantt/timeline chart

by Hyun-min & Tree ring 2023. 1. 29.

연륜연대분석 결과를 쉽게 표현하기 위해 새롭게 간트차트를 활용해서 그래프를 작성

 

그래프에서 표현하고자 항목은

1. 심변재 구분

2. 연륜수

3. 최외각연륜 표기

 

library(ggplot2)
library(ggalt)
library(dplyr)


setwd("c:/R/1400_R_Timeline/")

den <- read.csv("dendro.csv", stringsAsFactors=FALSE)

arrange(den, no) %>%
  mutate(name=factor(name, levels=c(" ", rev(name), "  "))) -> den
  
treerings <- function(x) {
  x <- sprintf("%d", round(x))
  x
}

gg <- ggplot()
gg <- gg + geom_segment(data=den, size=10, color="#D2691E",
                        aes(x=start_year, xend=end_year, y=name, yend=name))
gg <- gg + geom_segment(data=den, size=10, color="#FFDAB9",
                        aes(x=end_year-sap, xend=end_year, y=name, yend=name))
gg <- gg + geom_text(data=den, color="black", size=4, vjust=0.5, family="",
                     aes(x=end_year+2, y=name, label=treerings(end_year)))
gg <- gg + geom_text(data=den, color="black", size=4, vjust=0.5, family="",
                     aes(x=start_year-2, y=name, label=treerings(start_year)))
gg <- gg + geom_text(data=filter(den, sap<=1), color="black", size=4, vjust=0.5, family="",
                     aes(x=((end_year+start_year)/2), y=name, label=treerings(lengh)))
gg <- gg + geom_text(data=filter(den, sap>=1), color="black", size=4, vjust=0.5, family="",
                     aes(x=end_year-(sap/2), y=name, label=treerings(sap)))
gg <- gg + geom_text(data=filter(den, sap>=1), color="black", size=4, vjust=0.5, family="",
                     aes(x=((end_year-sap)/2), y=name, label=treerings(end_year-sap)))
gg <- gg + labs(x="Tree rings", y=NULL, subtitle="innermost ring ↔ outmost ring")
gg <- gg + theme_minimal()
gg <- gg + theme(axis.title.x = element_text(size=10,face='bold'))
gg <- gg + theme(plot.margin=margin(10,10,10,10))
gg <- gg + theme(plot.title=element_text(face="bold"))
gg <- gg + theme(plot.subtitle=element_text(face="bold", size=12, hjust = 0.5, margin=margin(b=2)))
gg <- gg + theme(plot.caption=element_text(size=7, margin=margin(t=12), color="#7a7d7e"))
gg

 

 

den <- read.csv("dendro3.csv", stringsAsFactors=FALSE)

arrange(den, no) %>%
  mutate(name=factor(name, levels=c(" ", rev(name), "  "))) -> den

treerings <- function(x) {
  x <- sprintf("%d", round(x))
  x
}

# double & triple bars

gg <- ggplot()
gg <- gg + geom_segment(data=den, size=5, color="#D2691E",
                        aes(x=start_year, xend=end_year, y=name, yend=name))
gg <- gg + geom_segment(data=den, size=5, color="#FFDAB9",
                        aes(x=end_year-sap, xend=end_year, y=name, yend=name))
gg <- gg + geom_text(data=den, color="black", size=3.5, vjust=0.3, family="",
                     aes(x=end_year+4, y=name, label=treerings(end_year)))
gg <- gg + geom_text(data=den, color="black", size=3.5, vjust=0.3, family="",
                     aes(x=start_year-5, y=name, label=treerings(start_year)))
gg <- gg + geom_text(data=filter(den, sap<=1), color="black", size=3.5, vjust=0.3, family="",
                     aes(x=((end_year+start_year)/2), y=name, label=treerings(lengh)))
gg <- gg + geom_text(data=filter(den, sap>=1), color="black", size=4, vjust=0.3, family="",
                     aes(x=end_year-(sap/2), y=name, label=treerings(sap)))
gg <- gg + geom_text(data=filter(den, sap>=1), color="black", size=4, vjust=0.3, family="",
                     aes(x=((end_year-sap)/2), y=name, label=treerings(end_year-sap)))
gg <- gg + geom_text(data=filter(den, mark=="★만재완성"), color="black", size=4, vjust=0.2, family="",
                     aes(x=end_year+17, y=name, label=mark))
gg <- gg + geom_text(data=filter(den, mark=="☆조재완성"), color="black", size=4, vjust=0.2, family="",
                     aes(x=end_year+17, y=name, label=mark))
gg <- gg + labs(x="Year", y=NULL, subtitle="innermost ring ↔ outmost ring")
gg <- gg + theme_minimal()
gg <- gg + theme(axis.title.x = element_text(size=10,face='bold'))
gg <- gg + theme(plot.margin=margin(10,50,10,10))
gg <- gg + theme(plot.title=element_text(face="bold"))
gg <- gg + theme(plot.subtitle=element_text(face="bold", size=12, hjust = 0.5, margin=margin(b=2)))
gg <- gg + theme(plot.caption=element_text(size=7, margin=margin(t=12), color="#7a7d7e"))
gg

 

참고사이트

https://rud.is/b/2016/06/16/your-data-vis-spidey-sense-the-need-for-a-robust-utility-belt/