2013-12-18 146 views
-2

有人可以幫助將圖形的圖例更改爲連續顏色圖例,就像第二張圖片中的圖例。比例顏色圖例

library(demography) 

plot.demogdata(portugal,series='male') 
legend("bottomright",legend=unique(portugal$year), 
    col=rainbow(length(portugal$year)*1.25), ncol=5, pch=19, 
    title="Year", cex=0.5) 

enter image description here

enter image description here

+2

下圖是用ggplot2生成的。您可以輕鬆地使用ggplot2製作上圖,並自動獲得連續的色階。 – Roland

+2

你如何獲得對象「葡萄牙」?沒有這個我們不能重現你的問題。 – SlowLearner

+0

使用底圖不太容易。提示:您應該使用'layout'準備圖例區域,然後使用'image'和'axis'。 (ggplot2/lattice) – agstudy

回答

1

試試這個。不是高科技,但可以滿足您的需求,並且不需要額外的軟件包。 複製&粘貼到R控制檯,看看它是如何工作的。

# not using layout function 

# colors 
n <- 1:50 
col <- rainbow(length(n)) 

# expanding right margin 
par(mar=c(5,4,4,8),xpd=T) 

# simulating any graph 
plot(n,n*2,type='n',ylab="fun(x,n)") 

# making DYI scale and legend 
# scale 
x <- rep(par('usr')[2]*1.1,length(n)+1) 
y <- seq(f=par('usr')[3],t=par('usr')[4],length.out=length(n)+1) 
nul <- sapply(n,FUN=function(n,col,x,y,...){lines(x=x[c(n,n+1)],y=y[c(n,n+1)],col=col[n],...)},lwd=30,lend="butt",col=col,x=x,y=y) 
# labels 
ty <- y[n]+diff(y)/2 
tx <- rep(par('usr')[2]*1.25,length(n)) 
t <- paste("Lab",n) 
ti <- pretty(n) 
text(x=tx[ti],y=ty[ti],t[ti]) 
+0

當我運行指令圖(n,n * 2)時,type ='n',ylab =「fun(x,n)」)情節很清楚。 – user2898085

+0

@ user2898085是的,那是因爲我的代碼只是一個例子,如果你想在你的情況下使用它,你必須根據你的需要調整代碼,我有舊的R版本,沒有機會安裝你使用的軟件包,所以我幫助了自己的情節。 –