1. 程式人生 > >R語言-增加圖例

R語言-增加圖例

圖片 cit highlight 內容 top tro ear gree 分享

legend()函數

> plot(rain$Tokyo,type="l",col="red", 
+ ylim=c(0,300), 
+ main="Monthly Rainfall in major cities", 
+ xlab="Month of Year", 
+ ylab="Rainfall (mm)", 
+ lwd=2) 
> lines(rain$NewYork,type="l",col="blue",lwd=2) 
> lines(rain$London,type="l",col="green",lwd=2) 
> lines(rain$Berlin,type="l",col="orange",lwd=2) 

技術分享圖片

1.第一種樣式

> legend("topright",                                    #圖例位置為右上角
+ legend=c("Tokyo","NewYork","London","Berlin"),        #圖例內容
+ col=c("red","blue","green","orange"),                 #圖例顏色
+ lty=1,lwd=2)                                          #圖例大小

技術分享圖片

2.第二種樣式

> legend("top",                                        #圖例位置為上方
+ legend=c("Tokyo","NewYork","London","Berlin"), 
+ ncol=4, 
+ cex=0.8, 
+ bty="n", 
+ col=c("red","blue","green","orange"), 
+ lty=1,lwd=2) 

技術分享圖片

R語言-增加圖例