2013-06-27 168 views
10

我試圖重現一個帶漸變圓的Stephen Few圖形,演示了從上方出現光的硬連線假設。這裏是圓:繪製漸變圓

enter image description here

我怎樣才能重新呢?繪製圓圈並不算太壞,但添加漸變是我拋出的地方。我認爲網格可能會創造更清晰的東西,但這可能是我的錯誤觀念。

這裏是開始用畫圓:

## John Fox circle function 
source("http://dl.dropboxusercontent.com/u/61803503/wordpress/circle_fun.txt") 

par(mar=rep(1, 4), bg = "grey80") 
plot.new() 

for (i in seq(0, 1, by = .2)) { 
    for (j in seq(.6, 1, by = .1)) { 
     circle(i, j, .5, "cm", , 1) 
    } 
} 

相關問題:How to use R to build bubble charts with gradient fills

編輯:

想我會分享成果: enter image description here

而且here's the code

+0

如何順利,你需要的梯度是什麼? –

+0

足以保留錯覺,但您可以看到上面漸變中的線條。 –

+0

也許你可以創建幾行黑色到白色的漸變,然後在它們上面繪圖?這個問題漸變:http://stackoverflow.com/questions/11070101/2d-color-gradient-plot-in-r –

回答

9

隨着一些重複使用clip,你可以到達那裏。

# set up a blank plot 
par(mar=rep(0, 4)) 
par(bg="#cccccc") 
plot(NA,xlim=0:1,ylim=0:1) 

# define a function 
grad.circ <- function(centrex,centrey,radius,col,resolution) { 
    colfunc <- colorRampPalette(col) 
    shades <- colfunc(resolution) 

    for (i in seq_along(shades)) { 
    clip(
     centrex - radius, 
     centrex + radius, 
     (centrey + radius) - ((i-1) * (radius*2)/length(shades)), 
     (centrey + radius) - (i  * (radius*2)/length(shades)) 
     ) 
    symbols(
    centrex, 
    centrey, 
    circles=radius, 
    bg=shades[i], 
    fg=NA, 
    add=TRUE, 
    inches=FALSE 
     ) 
    } 
} 

# call the function 
grad.circ(0.5,0.5,0.5,c("black", "white"),300) 

結果:

enter image description here

EDIT(由泰勒林克):

我想補充我用來複製圖像的其餘代碼:

FUN <- function(plot = TRUE, cols = c("black", "white")) { 
    plot(NA, xlim=0:1, ylim=0:1, axes=FALSE) 
    if (plot) { 
     grad.circ(0.5, 0.5, 0.5, cols, 300) 
    } 
} 

FUN2 <- function(){ 
    lapply(1:3, function(i) FUN(,c("white", "black"))) 
    FUN(F) 
    lapply(1:3, function(i) FUN()) 
} 


X11(10, 4.5) 
par(mfrow=c(3, 7)) 
par(mar=rep(0, 4)) 
par(bg="gray70") 
invisible(lapply(1:3, function(i) FUN2())) 
+0

+1非常好。非常感謝。 –

+0

我從未見過使用過的剪輯。我試圖擴展這一點。我怎樣才能讓這些圈子更小?換句話說,什麼是控制圓的半徑? –

+1

@TylerRinker - 我已經概括了代碼,所以希望它是有道理的。 – thelatemail

2

你可以這樣做使用(不在CRAN)包zernike。它旨在生成與Zernike多項式相關的各種圖像,大量用於天文學系統的光學&。你想要的圖像幾乎是第二個Zernike術語。

作者是作者:M.L. Peck([email protected]);我忘記了R軟件包在網絡上的位置。

+0

以下是鏈接:http://wildlife-pix.com/rpackages/,但我無法將其應用於此問題。它建立在R 3.0.0之前。 –

+0

@TylerRinker感謝您找到它。我從來沒有建立它;只需使用包含的R功能即可。 –

+0

哦,我陷入了困境。 :) –

3

下面是使用柵格和rasterImage版本:

image <- as.raster(matrix(seq(0,1,length.out=1001), nrow=1001, ncol=1001)) 
tmp <- (row(image) - 501) ^2 + (col(image) - 501)^2 
image[tmp > 500^2] <- NA 

image2 <- as.raster(matrix(seq(1,0, length.out=1001), nrow=1001, ncol=1001)) 
image2[ tmp > 500^2 ] <- NA 

image3 <- row(image) + col(image) 
image3 <- image3/max(image3) 
image3[tmp>500^2] <- NA 
image4 <- 1-image3 
image3 <- as.raster(image3) 
image4 <- as.raster(image4) 

plot(0:1, 0:1, type='n', asp=1,ann=FALSE,axes=FALSE) 
rect(0,0,1,1, col='grey') 
rasterImage(image, 0.2, 0.2, 0.3, 0.3) 
rasterImage(image2, 0.6, 0.6, 0.7, 0.7) 
rasterImage(image3, 0.6, 0.3, 0.7, 0.4) 
rasterImage(image4, 0.3, 0.7, 0.4, 0.8) 

陰影的其他方向可以通過改變數學一點進行。

+0

這也很好用+1 –

2

下面是使用sprgeos(類似的應用程序herehere)的方法。

library(sp) 
library(rgeos) 
library(raster) 
  1. 通過緩衝點創建兩個組9圈,然後繪製他們的工會組建繪圖區。

    b <- gBuffer(SpatialPoints(cbind(rep(1:3, 3), rep(1:3, each=3))), TRUE, 
          width=0.45, quadsegs=100) 
    b2 <- gBuffer(SpatialPoints(cbind(rep(5:7, 3), rep(1:3, each=3))), TRUE, 
           width=0.45, quadsegs=100) 
    
    plot(gUnion(b, b2), border=NA) 
    
  2. 逐步通過多邊形並提取它們的邊界框。

    bb <- sapply([email protected], bbox) 
    bb2 <- sapply([email protected], bbox) 
    
  3. 繪製疊加段以模擬梯度。

    segments(rep(bb[1,], each=1000), 
         mapply(seq, bb[2,], bb[4,], len=1000), 
         rep(bb[3,], each=1000), col=gray.colors(1000, 0)) 
    
    segments(rep(bb2[1,], each=1000), 
         mapply(seq, bb2[2,], bb2[4,], len=1000), 
         rep(bb2[3,], each=1000), col=rev(gray.colors(1000, 0))) 
    
  4. 差異的SpatialPolygon對象的工會並繪製多邊形差分掩蓋了非圓的區域。

    plot(gDifference(as(extent(par('usr')), 'SpatialPolygons'), gUnion(b, b2)), 
        col='gray80', border='gray80', add=TRUE) 
    
  5. 對於獎圓平滑度,再次繪製圓圈,顏色等於背景顏色。

    plot(gUnion(b, b2), border='gray80', lwd=2, add=TRUE) 
    

gradient bubbles

+0

看起來非常有希望光滑。第4步中的「範圍」功能在哪裏? –

+0

@泰勒 - '光柵'...對不起。 – jbaums

+0

不錯,工作得很好,故障也很好。 –