2013-02-05 74 views
1

我怎麼能彎曲默認彩虹R.例如,看看這個代碼和圖像它產生:翹曲R中的彩虹

x = seq(0,50,by=0.005) 
y = runif(length(x),2,5) 
colors = rainbow(length(x)) 
plot(x,y,cex=0.2,pch=16,col=colors) 

enter image description here

我想改變這種所以沒有那麼多綠色。應該有像黃色,青色和藍色一樣多的綠色。我也想去除洋紅色(最右邊的「紅色」顏色)。我怎麼能這樣做呢?

回答

8

如果跳過綠色明確了選項產生的,你接近你想要什麼,我認爲:

par(mfrow=c(2,1)) 
par(mar=c(2,1,3,1)) 

x = seq(0,50,by=0.005) 
y = runif(length(x),2,5) 
colors = rainbow(length(x)) 
plot(x,y,cex=0.2,pch=16,col=colors) 
title(main="old") 

crp.rg <- colorRampPalette(c("red","yellow","cyan","blue","red")) 
colors = crp.rg(length(x)) 
plot(x,y,cex=0.2,pch=16,col=colors) 
title(main="new") 

enter image description here

編輯

你也可以手動編輯每個色彩過渡使它們具有不同的長度:

par(mfrow=c(2,1)) 
par(mar=c(2,1,3,1)) 

x = seq(0,50,by=0.005) 
y = runif(length(x),2,5) 
colors = rainbow(length(x)) 
plot(x,y,cex=0.2,pch=16,col=colors) 
title(main="old") 


crp.step1 <- colorRampPalette(c("red","yellow")) 
crp.step2 <- colorRampPalette(c("yellow","green")) 
crp.step3 <- colorRampPalette(c("green","cyan")) 
crp.step4 <- colorRampPalette(c("cyan","blue")) 
crp.step5 <- colorRampPalette(c("blue","red")) 

colors <- c(
      crp.step1(2666), 
      crp.step2(1000), 
      crp.step3(1000), 
      crp.step4(2666), 
      crp.step5(2667) 
     ) 

plot(x,y,cex=0.2,pch=16,col=colors) 
title(main="new") 

enter image description here

+0

好主意......但現在沒有綠色......我仍然想要綠色。 – CodeGuy

+0

@CodeGuy - 看到編輯 - 不是很整潔,但應該允許更多的靈活性。 – thelatemail