在r中绘制不平滑的正态分布

时间:2017-06-23 07:12:47

标签: r

亲爱的,我有气温数据,我需要绘制曲线不平滑的三个选定年份的正态分布。我正在寻找的数字类似于图2 here,在那里我可以看到每条曲线的温度波动(即噪音)。

以下是在线数据和代码的示例:

Cowtan<-read.table("http://www-users.york.ac.uk/~kdc3/papers/coverage2013/had4_krig_v2_0_0.txt", header=F)
names(Cowtan)<-c("Year", "Temperature", "Uncertainty1", "Uncertainty2", "Uncertainty3") #Name the columns

S1850s<-subset(Cowtan, Year<1860) #Get subsets of each decade
S1950s<-subset(Cowtan, Year>=1950 & Year<1960)
S2007<-subset(Cowtan, Year>=2007 & Year<2017)
D1850s=density(S1850s$Temperature) #Get the density kernals
D1950s<-density(S1950s$Temperature)
D2007<-density(S2007$Temperature)
plot(D1850s, main="Bell curve of  temperatures during selected decades", xlab="Temperature anomalies (ºC)",xlim=c(-1.1,1.3), ylim=c(0,3), lwd=1.5)
points(D2007, type="l",col="red", lwd=1.5)
points(D1950s, type="l",col="blue", lwd=1.5)
legend("topleft", legend=c("1850s", "1950s", "2007-2016"), col=c("black", "blue", "red"), lwd=2)

2 个答案:

答案 0 :(得分:1)

如何生成正态分布的点并将它们添加到图中?

Cowtan<-read.table("http://www-users.york.ac.uk/~kdc3/papers/coverage2013/had4_krig_v2_0_0.txt", header=F)
names(Cowtan)<-c("Year", "Temperature", "Uncertainty1", "Uncertainty2", "Uncertainty3") #Name the columns

S1850s<-subset(Cowtan, Year<1860) #Get subsets of each decade
S1950s<-subset(Cowtan, Year>=1950 & Year<1960)
S2007<-subset(Cowtan, Year>=2007 & Year<2017)
D1850s=density(S1850s$Temperature) #Get the density kernals
D1950s<-density(S1950s$Temperature)
D2007<-density(S2007$Temperature)
plot(D1850s, main="Bell curve of  temperatures during selected decades", xlab="Temperature anomalies (ºC)",xlim=c(-1.1,1.3), ylim=c(0,3), lwd=1.5)
points(D2007, type="l",col="red", lwd=1.5)
points(D1950s, type="l",col="blue", lwd=1.5)

norm.dist <- rnorm(1000000, 0, 0.3)
points(density(norm.dist), type = "l", col="green")


legend("topleft", legend=c("1850s", "1950s", "2007-2016", "norm dist"), col=c("black", "blue", "red", "green"), lwd=2)

答案 1 :(得分:1)

这是一个&#34;不平滑曲线的例子&#34;类似于图2所示。

set.seed(1)
x <- rnorm(10000)
plot(density(x, bw=0.01))

enter image description here

如您所见,我将平滑带宽bw设置为一个非常小的值,以便密度图看起来像#34;未平滑&#34;。该图中的关键点是大量观测的可用性。 使用您的数据,最终的情节如下:

enter image description here

我希望这可以帮到你。

P.S。在上图中,我使用了kernel="epanechnikov"函数中的density选项。