gpt4 book ai didi

r - 如何使用 R 构建带有渐变填充的气泡图

转载 作者:行者123 更新时间:2023-12-03 03:03:39 25 4
gpt4 key购买 nike

我使用下面的 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但我想要具有 3D 表面的气泡,例如渐变填充和阴影。就像下面的图表一样。 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()

最佳答案

这是一个解决方案(受到 @Edward 的 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

关于r - 如何使用 R 构建带有渐变填充的气泡图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11618991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com