如何绘制给定范围的公式?

时间:2018-05-30 11:54:09

标签: r plot ggplot2 range formula

我希望绘制以下内容:

L<-((2*pi*h*c^2)/l^5)*((1/(exp((h*c)/(l*k*T)-1))))

l之外的所有变量都是常量:

T<-6000
h<-6.626070040*10^-34
c<-2.99792458*10^8
k<-1.38064852*10^-23

l的范围为20*10^-92000*10^-9

我已经尝试了l<-seq(20*10^-9,2000*10^-9,by=1*10^-9),但这并没有给我我期望的结果。

在R中是否有一个简单的解决方案,或者我是否必须尝试使用​​其他语言?

谢谢。

1 个答案:

答案 0 :(得分:2)

查看spectral radiance equation维基百科页面,您的公式似乎有些偏差。您的公式会增加pi(不确定是否有意),-1位于exp内,而非外部:

L <- ((2*pi*h*c^2)/l^5)*((1/(exp((h*c)/(l*k*T)-1))))

以下是更正后的公式。另请注意,我已将其转换为参数l的函数,因为这是一个变量:

T <- 6000                # Absolute temperature
h <- 6.626070040*10^-34  # Plank's constant
c <- 2.99792458*10^8     # Speed of light in the medium
k <- 1.38064852*10^-23   # Boltzmann constant

L <- function(l){((2*h*c^2)/l^5)*((1/(exp((h*c)/(l*k*T))-1)))}

# Plotting
plot(L, xlim = c(20*10^-9,2000*10^-9),
     xlab = "Wavelength (nm)",
     ylab = bquote("Spectral Radiance" ~(KW*sr^-1*m^-2*nm^-1)),
     main = "Plank's Law",
     xaxt = "n", yaxt = "n")
xtick <- seq(20*10^-9, 2000*10^-9,by=220*10^-9)
ytick <- seq(0, 4*10^13,by=5*10^12)
axis(side=1, at=xtick, labels = (1*10^9)*seq(20*10^-9,2000*10^-9,by=220*10^-9))
axis(side=2, at=ytick, labels = (1*10^-12)*seq(0, 4*10^13,by=5*10^12))

enter image description here

上面的情节不错,但我认为我们可以用ggplot2

做得更好
h <- 6.626070040*10^-34  # Plank's constant
c <- 2.99792458*10^8     # Speed of light in the medium
k <- 1.38064852*10^-23   # Boltzmann constant

L2 <- function(l, T){((2*h*c^2)/l^5)*((1/(exp((h*c)/(l*k*T))-1)))} # Plank's Law
classical_L <- function(l, T){(2*c*k*T)/l^4} # Rayleigh-Jeans Law

library(ggplot2)
ggplot(data.frame(l = c(20*10^-9,2000*10^-9)), aes(l)) +
  geom_rect(aes(xmin=390*10^-9, xmax=700*10^-9, ymin=0, ymax=Inf), 
            alpha = 0.3, fill = "lightblue") +
  stat_function(fun=L2, color = "red", size = 1, args = list(T = 3000)) +
  stat_function(fun=L2, color = "green", size = 1, args = list(T = 4000)) +
  stat_function(fun=L2, color = "blue", size = 1, args = list(T = 5000)) +
  stat_function(fun=L2, color = "purple", size = 1, args = list(T = 6000)) +
  stat_function(fun=classical_L, color = "black", size = 1, args = list(T = 5000)) +
  theme_bw() +
  scale_x_continuous(breaks = seq(20*10^-9, 2000*10^-9,by=220*10^-9),
                     labels = (1*10^9)*seq(20*10^-9,2000*10^-9,by=220*10^-9),
                     sec.axis = dup_axis(labels = (1*10^6)*seq(20*10^-9,2000*10^-9,by=220*10^-9),
                                         name = "Wavelength (\U003BCm)")) +
  scale_y_continuous(breaks = seq(0, 4*10^13,by=5*10^12),
                     labels = (1*10^-12)*seq(0, 4*10^13,by=5*10^12),
                     limits = c(0, 3.5*10^13)) +
  labs(title = "Black Body Radiation described by Plank's Law", 
       x = "Wavelength (nm)",
       y = expression("Spectral Radiance" ~(kWsr^-1*m^-2*nm^-1)),
       caption = expression(''^'\U02020' ~'Spectral Radiance described by Rayleigh-Jeans Law, which demonstrates the ultraviolet catastrophe.')) +
  annotate("text", 
           x = c(640*10^-9, 640*10^-9, 640*10^-9, 640*10^-9, 
                 150*10^-9, (((700-390)/2)+390)*10^-9, 1340*10^-9), 
           y = c(2*10^12, 5*10^12, 14*10^12, 31*10^12, 
                 35*10^12, 35*10^12, 35*10^12), 
           label = c("3000 K", "4000 K", "5000 K", "6000 K", 
                    "UV", "VISIBLE", "INFRARED"),
           color = c(rep("black", 4), "purple", "blue", "red"),
           alpha = c(rep(1, 4), rep(0.6, 3)),
           size = 4.5) +
  annotate("text", x = 1350*10^-9, y = 23*10^12, 
           label = deparse(bquote("Classical theory (5000 K)"^"\U02020")),
           color = "black", parse = TRUE)

备注:

  1. 我还通过将绝对温度L2变为
  2. 来创建T
  3. 对于每个T,我使用不同的颜色绘制函数L2以进行表示。我还添加了classical_L函数来演示光谱辐射的经典理论
  4. geom_rect为&#34; VISIBLE&#34;创建浅蓝色阴影区域。光波长范围
  5. scale_x_continuous设置x轴的中断,而labels设置轴刻度标签。请注意,我已将seq乘以(1*10^9),将单位转换为纳米(nm)。添加第二个x轴以显示千分尺
  6. 类似地,scale_y_continuous设置y轴的中断和刻度标签。在这里,我乘以(1*10^-12)(1*10^(-3-9)),将瓦特(W)转换为千瓦(kW),从逆米(m ^ -1)转换为反纳米(nm ^ -1)
  7. bquote在y轴标签
  8. 中正确显示上标
  9. annotate设置曲线标签的坐标和文字。我还为&#34; UV&#34;,&#34; VISIBLE&#34;添加了标签。和&#34; INFRARED&#34;光波长
  10. <强> GGPLOT2

    enter image description here

    来自维基百科的剧情:

    enter image description here

    图片来源:https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Black_body.svg/600px-Black_body.svg.png