R

미국 원자력 발전소 가동 현황 그래프 작성하기

이부일 2018. 10. 19. 10:05

# 미국의 원자력규제위원회
url <- "http://www.nrc.gov/…/r…/PowerReactorStatusForLast365Days.txt"


# 데이터 읽어오기
npower <- read.table(url, header = TRUE, sep = "|", stringsAsFactors = FALSE)


# 날짜 형태 바꾸기
npower$ReportDt <- as.Date(npower$ReportDt, format = "%m/%d/%Y")


# 유일한 목록 보기
unique(npower$Unit)


# 데이터 추출하기
some <- npower[npower$Unit == as.character(unique(npower$Unit)[1:12]) , ]


# 로케일 설정하기
Sys.setlocale(category = "LC_TIME", locale = "English")


# 그래프 작성하기
ggplot(data = some, mapping = aes(x = ReportDt, y = Power)) +
   geom_line(col = "red") +
   facet_wrap(~Unit, scale = "free_y", nrow = 4) +
   scale_x_date(date_labels = "%y-%b", date_breaks = "3 month") +
   labs(x = "",
         y = "Power",
         title = "Nuclear Power Reactor Status over Time",
         subtitle = "12 Samples of power reator in US",
         caption = "Source : Nuclear Regulatory Commission") +
   theme_bw() +
   theme(plot.title = element_text(face = "bold"),
           strip.background = element_rect(fill = "wheat"),
           axis.text = element_text(size = 8, face = "bold"),
           legend.title = element_blank())


Sys.setlocale()


자동 대체 텍스트를 사용할 수 없습니다.


[출처] 웹스크레이핑과 데이터분석, 곽기영 지음, 도서출판 청람, p23