2012-07-23 94 views
8

我使用下面的R代碼來構建氣泡圖。如何使用R來創建梯度填充的氣泡圖

pdf(file='myfigure.pdf',height=10,width=13) 
y<-c(123,92,104,23,17,89,13) 
x<-c(11,45,24,50,18,7,2) 
size<-c(1236,1067,1176,610,539,864,1026) 
radius<-sqrt(size/pi) 
col<-c(2,2,3,4,5,5,6) 
name<-c("Acura", "Alfa Romeo","AM General","Aston Martin Lagonda","Audi","BMW","Bugatti") 

symbols(x,y, circles=radius,fg="white",bg=col,ylim=c(-20,140)) 
text(x, y, name, cex=1.5,font=4) 
dev.off() 

enter image description here 但我想與三維表面的氣泡,說漸變填充和陰影。如下圖所示。 enter image description here

任何人都知道如何使用R來釋放它?謝謝!

感謝您提供的所有建議。最後,我試圖通過從黑暗到光線繪製多個圓圈來使其漸變填充的愚蠢方式。任何建議,使其更好?謝謝! enter image description here

makeTransparent<-function(someColor, alpha) 
{ 
newColor<-col2rgb(someColor) 
apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2],           blue=curcoldata[3],alpha=alpha,maxColorValue=255)}) 
} 

pdf(file='myfigure.pdf',height=10,width=13) 
y<-c(123,92,104,23,17,89,13) 
x<-c(11,45,24,50,18,7,2) 
size<-c(1236,1067,1176,610,539,864,1026) 
radius<-sqrt(size/pi) 
col<-c(2,2,3,4,5,5,6) 
name<-c("Acura", "Alfa Romeo","AM General","Aston Martin Lagonda","Audi","BMW","Bugatti") 


x2<-c() 
y2<-c() 
circles<-c() 
bg<-c() 
fg<-c() 

num<-30 
radius_min<-0.3 
alpha_min<-40 
alpha_max<-180 

for (i in 1:num){ 

x2<-c(x2,x) 
y2<-c(y2,y) 
circles<-c(circles,radius*(radius_min+(i-1)*(1-radius_min)/num)) 
bg<-c(bg,makeTransparent(col,alpha=alpha_max-(i-1)*(alpha_max-alpha_min)/num)) 
if(i!=num){fg<-c(fg,makeTransparent(col,alpha=alpha_max-(i-1)*(alpha_max-alpha_min)/num))}else{fg<-c(fg,rep('white',length(x)))} 


} 




symbols(x2,y2,circles=circles,fg=fg,bg=bg) 
text(x, y, name, cex=1.5,font=4) 
dev.off() 
+6

如果你喜歡的那種效果,R可能不適合你的工具。 R中的圖形功能是由那些真正不喜歡這種事情的人設計的,所以雖然可能,但我懷疑它不方便。 – joran 2012-07-23 19:22:16

+0

@Joran你的觀點很好,但這是一個有趣的問題,雖然我可能永遠不會使用它,但看到一個解決方案仍然很有趣(爲了讓一些喜歡這種事情的公司老闆印象深刻)。 – 2012-07-23 21:02:00

+1

@TylerRinker趣味性是一個意見問題,但我當然不是故意暗示這是一個糟糕的問題。只是嘗試保存OP一段時間,以防其他工具更合適。 – joran 2012-07-23 21:22:06

回答

6

這裏是一個解決方案(由@愛德華的解決方案this question啓發):

#First your data: 
y<-c(123,92,104,23,17,89,13) 
x<-c(11,45,24,50,18,7,2) 
size<-c(1236,1067,1176,610,539,864,1026) 
radius<-sqrt(size/pi) 
col<-c(2,2,3,4,5,5,6) 
name<-c("Acura", "Alfa Romeo","AM General","Aston Martin Lagonda","Audi","BMW","Bugatti") 

#Then a simple function to draw a circle based on its center and its radius: 
circle <- function (r, x0, y0, col){ 
    t <- seq(0, 2 * pi, by = 0.01) 
    x <- r * cos(t) + x0 
    y <- r * sin(t) + y0 
    lines(x, y, col=col) 
    } 

#This is a smoothing factor: 
sm <- 500 

#The asp parameter is important here since we are actually drawing the circles and not plotting some circle symbols: if asp is not equal to 1 they will appear as ellipse. 
plot(x,y,type="n",asp=1) 

#This can probably be vectorized but I'm not a good vectorizer so if anyone wants to give it a try: 
for(j in 1:length(x)){ 
    radius[j]*sm:1/sm -> radiuses 
    colorRampPalette(c(palette()[col[j]], "white"))->col_grad 
    col_grad(length(radiuses))->colx 
    for(i in 1:length(radiuses)){circle(radiuses[i], x[j], y[j], col=colx[i])} 
    } 

text(x, y, name, cex=1.5,font=4) 

查找有關此功能如何工作的更多信息,請參閱?colorRampPalette

enter image description here

編輯:與陰影

offset<-c(2,-2) #Offset of the shadow circles 
library(scales) #For function alpha 

plot(x,y,type="n",asp=1) 

for(j in 1:length(x)){ 
    radius[j]*sm:1/sm -> radiuses 
    colorRampPalette(c(palette()[col[j]], "white"))->col_grad 
    col_grad(length(radiuses))->colx 
    for(i in 1:length(radiuses)){circle(radiuses[i], x[j]+offset[1], y[j]+offset[2], col=alpha("grey90",0.1))} #the alpha, the nuance of grey can be tweaked with obviously for the desired effect 
    for(i in 1:length(radiuses)){circle(radiuses[i], x[j], y[j], col=colx[i])} 
    } 

text(x, y, name, cex=1.5,font=4) 

enter image description here

+0

不錯。也許你也可以放下陰影。 – 2012-08-09 07:47:42

+0

確實(通過給x和y添加一個偏移量),但由於較亮區域位於我的「球體」的中心,因此光線與屏幕垂直,因此陰影應該被球體本身隱藏,因此根本不會看到在所有的邏輯:) – plannapus 2012-08-09 07:51:17

+0

也許你可以想象在另一個角落有另一個弱光源,導致一個光影。 :) – 2012-08-09 07:54:37