'음영사각혀 추가 (shadowed box annotation)'에 해당되는 글 1건

  1. 2015.09.05 R ggplot2 텍스트(text), 선(line), 화살표(arrow), 음영사각형(shadowed box), 제목(title) 주석달기 : annotate()

그동안 R로 다양한 그래프를 그리는 방법을 알아보았습니다.  R로 그래프를 그렸다면, 보는 이의 가독성, 해석의 용이성을 높여주기 위해서 그래프 위에 텍스트, 가로선/세로선/대각선, 화살표, 음영 사각형, 제목 등과 같이 추가로 정보를 제공하거나 강조를 하고 싶은 부분에 주석을 달고 싶을 때가 있습니다.

 

 

- 텍스트 : annotate("text")

 

- 가로선/세로선/대각선 : geom_vline(), geom_hline(), geom_abline()

 

- 화살표 : annotate("segment", arrow=arrow()) , with grid package


- 음영 사각형 : annotate("rect")


- 제목 : ggtitle()

 

 

매번의 R 그래프/시각화 포스팅마다 주석 다는 방법을 간간이 곁들여서 소개해드리기는 했는데요, 이번 포스팅에서는 주석 다는 방법에 대해서 포괄적이고 종합적으로 정리를 해서 바로 찾아보기 편하도록 정리를 해보았습니다.

 

 

예제로 사용할 데이터는 Base Package에 내장되어 있는 iris 데이터 프레임의 Petal.Width, Petal.Length, Species 의 세개 변수를 사용하겠습니다. (iris 데이터셋은 데이터 마이닝 실습에 아주 많이 사용되는 데이터셋으로서, iris 꽃 품종 중 setosa 50개, versicolor 50개, virginica 50개를 꽃잎의 넓이와 길이를 측정해놓은 데이터셋입니다)

 

 

 

> str(iris)
'data.frame':	150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... 

 

 

ggplot2는 별도의 설치 및 호출이 필요한 패키지이므로 아래의 절차를 먼저 실행하시기 바랍니다.

 

> install.packages("ggplot2")
Installing package into ‘C:/Users/user/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.2/ggplot2_1.0.1.zip'
Content type 'application/zip' length 2676992 bytes (2.6 MB)
downloaded 2.6 MB

package ‘ggplot2’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\user\AppData\Local\Temp\RtmpEfAwCj\downloaded_packages
> library(ggplot2) 

 

(1) iris의 Petal.Width를 x축으로, Petal.Length를 y축으로 하고, 품종(Species)에 따라서 색깔을 달리해서 산포도(scatter plot)을 그려보겠습니다.

 

> # scatter plot of iris dataset
> a1 <- ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Species)) + 
+   geom_point(colour="grey", shape=21, size=6) +
+   scale_fill_brewer(palette="Reds")
> 
> a1
 

 

 

 

 

(2) 텍스트(text) 추가 : annotate("text")

 

> # 텍스트(text) 추가 : annotate("text")
> 
> a2 <- a1 + 
+   annotate("text", x=0.25, y=2.4, label="Setosa", size=7) + # text annotation
+   annotate("text", x=1.3, y=3.3, label="Versicolor", size=7) + 
+   annotate("text", x=1.7, y=6.8, label="Virginica", size=7)
> 
> a2

 

 

 

 

 

 

(3) 선(line) 추가 : geom_vline(), geom_hline(), geom_abline()

 

> # 선(line) 추가 : geom_vline(), geom_hline(), geom_abline()
> 
> a3 <- a2 + 
+   geom_hline(yintercept=2.6, colour="grey", lty="dashed", size=1) + # horizontal line
+   geom_hline(yintercept=4.9, colour="grey", lty="dashed", size=1) + 
+ 
+   geom_vline(xintercept=0.8, colour="grey", lty="dashed", size=1) + # vertical line
+   geom_vline(xintercept=1.75, colour="grey", lty="dashed", size=1) + 
+   
+   geom_abline(intercept=8, slope=-2.1, colour="red", lty="dotted", size=1.5) # abline
>   
> a3

 

 

 

 

 

(4) 화살표(arrow) 추가 : annotate("segment")

 

단, grid 패키지를 호출해서 사용해야 합니다.

 

> # 화살표(arrow) 추가 : annotate("segment")
> library(grid) # grid 패키지 호출
> 
> a4 <- a3 + 
+   annotate("segment", x=2, xend=2.1, y=2, yend=3.5, size=1.5, colour="red", arrow=arrow())
> 
> a4
> 
> 
> # 텍스트 추가 : annotate("text")
> a5 <- a4 + 
+   annotate("text", x=2, y=1.8, size=6, colour="red", label="y=8 - 2.1x")
> 
> a5

 

 

 

 

 

 

 

(5) 음영 사각형(shadowed box) 추가 : annotate("rect")

 

> # 음영 사각형(shadowed box) 추가 : annotate("rect")
> a6 <- a5 + 
+   annotate("rect", xmin=0, xmax=0.8, ymin=0, ymax=2.6, alpha=0.1, fill="red") + 
+   annotate("rect", xmin=0.8, xmax=1.75, ymin=2.6, ymax=4.9, alpha=0.2, fill="red") + 
+   annotate("rect", xmin=1.3, xmax=2.7, ymin=4.3, ymax=7.2, alpha=0.3, fill="red")
> 
> a6

 

 

 

 

 

(6) 제목(title) 추가 : ggtitle()

 

> # 제목 추가 : ggtitle()
> 
> a7 <- a6 + 
+   ggtitle("Annotation of Text, Line, Arrow, Shadowed Box, Title")
> 
> a7

 

 

 

 

많은 도움 되었기를 바랍니다.

 

이번 포스팅이 도움이 되었다면 아래의 '공감 ~♡' 단추를 꾸욱 눌러주세요.^^

 

728x90
반응형
Posted by Rfriend
,