R에서 애니메이션 크리스마스 트리 만들기

R-Blogger · 블로그·해설 · 2024-12-23

R-Blogger블로그·해설한국어2024-12-23

R에서 애니메이션 크리스마스 트리 만들기

크리스마스 트리 애니메이션 만들기 크리스마스가 내일이기 때문에, {ggplot2}, {sf}, 그리고 {gganimate}를 사용해 애니메이션 크리스마스 트리를 만들기로 했습니다. 먼저 트리 모양을 만들 수 있는 sf 폴리곤을 준비합니다. library(ggplot2) library(gganimate) library(sf) tree_coords = list( matrix( c(-4, 0, -2.22, 2, -3.5, 2, -1.5, 4, -2.5, 4, -0.8, 6, -1.5, 6, 0, 8, 1.5, 6, 0.8, 6, 2.5, 4, 1.5, 4, 3.5, 2, 2.22, 2, 4, 0, -4, 0), ncol=2, byrow=TRUE ) ) tree = st_polygon(tree_coords) gg_tree = ggplot() + geom_sf(aes(), data=tree) gg_tree 트리 모양이 완성되었습니다. 이제 트리를 크리스마스 분위기에 맞게 꾸며보겠습니다. 색상 변경: fill = "forestgreen", color = "darkgreen" 나무 기둥 추가: geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = -2, ymax = 0), fill = "saddlebrown", color = "sienna4") 별 추가: geom_point(aes(x = 0, y = 8), color = "gold", shape = 8, size = 7, stroke = 3) 축소선 제거: theme_void() 경계 설정: coord_sf(xlim = c(-6, 6), ylim = c(-4, 10)) 크리스마스 메시지: annotate("text", x = 0, y = 9.5, label = "Merry Christmas \n From Jumping Rivers!", size = 6) gg_tree = ggplot() + geom_sf(aes(), data=tree, fill = "forestgreen", color = "darkgreen") + geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = -2, ymax = 0), fill = "saddlebrown", color = "sienna4") + geom_point(aes(x = 0, y = 8), color = "gold", shape = 8, size = 7, stroke = 3) + theme_void() + coord_sf(xlim = c(-6, 6), ylim = c(-4, 10)) + annotate("text", x = 0, y = 9.5, label = "Merry Christmas \n From Jumping Rivers!", size = 6) gg_tree 다음 단계에서는 {sf}를 다시 사용해 트리에 빛을 넣고, {gganimate}로 빛이 반짝이는 애니메이션을 만듭니다. # 트리 안에 랜덤 포인트 생성 points = st_sample(tree, 75) # 포인트 색상 벡터 생성 colours = sample(c("red", "yellow", "blue"), 75, replace = TRUE) gg_tree = ggplot() + geom_sf(aes(), data=tree, fill = "forestgreen", color = "darkgreen") + geom_sf(aes(), data=points, color = colours) + geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = -2, ymax = 0), fill = "saddlebrown", color = "sienna4") + geom_point(aes(x = 0, y = 8), color = "gold", shape = 8, size = 7, stroke = 3) + theme_void() + coord_sf(xlim = c(-6, 6), ylim = c(-4, 10)) + annotate("text", x = 0, y = 9.5, label = "Merry Christmas \n From Jumping Rivers!", size = 6) gg_tree 마지막으로 transition_time과 ease_aes를 이용해 빛이 반짝이는 애니메이션을 구현합니다. gg_tree + transition_time(1:75) + ease_aes('linear') 마지막으로 Jumping Rivers 팀에서 즐거운 크리스마스와 새해를 기원합니다! 이 글의 업데이트 및 수정 사항은 원본 게시물을 참고해 주시기 바랍니다.
원문 URL
전체 글은 원문 페이지에서 이어서 읽을 수 있습니다.
원문에서 전체 글 읽기
작성자
R-Blogger
출처
R-Blogger
플랫폼
R-Blogger
분류
블로그·해설
언어
한국어
발행일
2024-12-23