R画图- 带有颜色渐变的正态曲线

9
如何在R中使用渐变色制作曲线。看看这个火焰的图片enter image description here,它应该是这个样子的。我试着画一个正常的曲线,然后再画一个正常的曲线,但从技术上讲,你不能用一堆正常的曲线来制作这样的图形,因为它们不会在正确位置的两侧相交。我该如何在R中制作这样的图形?有什么想法吗?

我选择了Beta分布,因为它支持[0,1]范围内的取值。你可以通过修改a和b参数来使其峰值更加尖锐。只要保持它们相同,分布就是对称的,但如果你想要一个偏向于某一侧的分布,你可以让它们不同。 - IRTFM
1个回答

25

到目前为止,我能做到的最好的是:

 par(bg="black")
 plot(seq(0.15,0.85,by=0.01), 
      5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), 
      type="l" , ylim=c(0,700) )  # this just sets up the plotting framework.

 for( i in 1:200 ) { lines(x= seq(0.15,0.85,by=0.01), 
                           y= i*dbeta(seq(0.15,0.85,by=0.01),10,10 ), 
                     col= colorRampPalette(c("yellow", "orange", "red", "hotpink", 
                             "violet", "blue", "lightblue", "lightgreen", "darkgreen",
                             "black"))(200)[i], 
                      lwd=13) }
 par(bg="white")

我发现在该系列的开头放置“黑色”会给整体结果增添额外的“辉光”,但我不会发布那个结果。

enter image description here

-----------------

这是我开始的内容,然后下面出现了连续逼近和微调:
plot(seq(0.15,0.85,by=0.01), 5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), 
            type="l" , ylim=c(0,100))
for( i in seq(0.2, 5) ) { lines(seq(0.15,0.85,by=0.01), 
                     i*5*dbeta(seq(0.15,0.85,by=0.01),10,10 ) ) }

enter image description here

颜色相关:

plot(seq(0.15,0.85,by=0.01), 5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), type="l" ,
          ylim=c(0,130))
for( i in 1:35 ) {lines(seq(0.15,0.85,by=0.01), i*dbeta(seq(0.15,0.85,by=0.01), 10,10 ), 
                           col=colorRampPalette(c("yellow", "orange", "red", "violet", 
                                        "blue", "lightblue", "lightgreen"))(35)[i],
                   lwd=3) }

enter image description here

对于黑色背景和更浓密的颜色以及顶部的淡出效果:

 par(bg = 'black')
 plot(seq(0.15,0.85,by=0.01), 5*dbeta(seq(0.15,0.85,by=0.01),10,10 ), type="l", 
          ylim=c(0,130) )
 for( i in 1:35 ) { lines(seq(0.15,0.85,by=0.01), i*dbeta(seq(0.15,0.85,by=0.01),10,10), 
                          col=colorRampPalette(c("yellow", "orange", "red", "violet", 
                                "blue", "lightblue", "lightgreen", "darkgreen", 
                                 "black")) (35)[i], 
                           lwd=13) }

enter image description here

我注意到淡出至黑色的效果还控制了边缘的线条宽度。我没有预料到这一点,但似乎这是一个令人满意的特性。这里未提及的另一个方面是添加透明度的可能性。R RGB 函数中有一个 alpha 参数。
查找颜色名称的一个有用技巧:
grep("purple", colors(), value=TRUE)
 [1] "mediumpurple"  "mediumpurple1" "mediumpurple2" "mediumpurple3" "mediumpurple4"
 [6] "purple"        "purple1"       "purple2"       "purple3"       "purple4"      

如果您正在使用迭代来使渐变更加平滑,则还需要调整ylim参数:选择0.5^9*0.5^9/beta(10,10)*[iterations],因为在x=0.5时这将是最大值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接