본문 바로가기
4. 그래프 꾸미기(저수준 함수)/legend() - 범례

R 범례(legend) 넣는 방법 (위치,기호,색,크기)

by makhimh 2020. 6. 11.
반응형

R 범례(legend) 넣는 방법


legend(x, y = NULL, legend, fill = NULL, col = par("col"),

       border = "black", lty, lwd, pch,

       angle = 45, density = NULL, bty = "o", bg = par("bg"),

       box.lwd = par("lwd"), box.lty = par("lty"), box.col = par("fg"),

       pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd,

       xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1,

       adj = c(0, 0.5), text.width = NULL, text.col = par("col"),

       text.font = NULL, merge = do.lines && has.pch, trace = FALSE,

       plot = TRUE, ncol = 1, horiz = FALSE, title = NULL,

       inset = 0, xpd, title.col = text.col, title.adj = 0.5,

       seg.len = 2)


옵션이 많은데, 자주 쓰이는 것만 설명하겠습니다. 


x : 범례의 x 위치

y : 범례의 y 위치

fill : 범례기호 채우기 색

col : 범례기호 점,선 색

border : 범례 상자 테두리색

bg : 범례 상자 배경색

pch : 범례 기호 모양, 설정 안할경우 디폴트는 사각형.

cex : 범례 크기

box.lty : 범례 외곽선의 모양(0으로 할 경우 외곽선 없음)


범례의 위치를 설정하는 방법은 두 가지가 있는데 좌표를 입력하는 방법과 명령어를 입력하는 방법입니다. 명령어는 아래와 같습니다. 


"top"

"topleft"

"topright"

"bottom"

"bottomleft"

"bottomright"

"center"

"left"

"right"


제가 자주 사용하는 범례를 예시로 보여드리겠습니다.


data=rbind(A=c(1,2,3),B=c(2,4,5))


mybar=barplot(data,beside=T,ylim=c(0,10),names=c("a","b","c"),col=c("red","blue"),border="white")

text(x=mybar,y=data,labels=data,pos=3,col="black")

grid(nx=NA,ny=NULL)


legend("topright",legend=c("A","B"),fill=c("red","blue"),border="white",box.lty=0,cex=1.5)





반응형

댓글