1

I have this code:

`MyData <- read.csv2( file="C:/Users/Sysop/Desktop/R_Skaiciavimai/Sezonai.csv",header=T,sep=";") MyData

Data_1<-MyData$Sul11P
Data_2<-MyData$Sul11V
Data_3<-MyData$Sul11R
Data_4<-MyData$Sul11Z

D_adjust<-1

Data_1_density<- density(Data_1,adjust = D_adjust,na.rm = TRUE)
Data_2_density<- density(Data_2,adjust = D_adjust,na.rm = TRUE)
Data_3_density<- density(Data_3,adjust = D_adjust,na.rm = TRUE)
Data_4_density<- density(Data_4,adjust = D_adjust,na.rm = TRUE)

Meginio_Vieta<-'Šulinėlis T1-1'
DataBins<-seq(0, 12, by=0.5)

Col_1 <- rgb(1,0,0,0.4)
Col_2 <- rgb(0,0,1,0.4)
Col_3 <- rgb(0,1,0,0.4)


par( mfrow = c( 2, 2 ) )

myhist <- hist(Data_1,breaks=DataBins, plot=FALSE)
multiplier <- myhist$counts / myhist$density
mydensity <- Data_1_density
mydensity$y <- mydensity$y * multiplier[1]
plot(myhist,xlim =c(0,11),col=Col_2,xlab = expression(NH[4]*-N~ "koncentracija     mg/l"),main = Meginio_Vieta, panel.first = grid(nx = 10, ny = 10))
axis(1, at=c(1,3,5,7,9,11))
mtext("Pavasario sezonas",cex = 0.8,adj=0.5, padj=0)
lines(mydensity,col="red", lwd=2)

myhist <- hist(Data_2,breaks=DataBins, plot=FALSE)
multiplier <- myhist$counts / myhist$density
mydensity <- Data_2_density
mydensity$y <- mydensity$y * multiplier[1]
plot(myhist,xlim =c(0,11),col=Col_2,xlab = expression(NH[4]*-N~ "koncentracija mg/l"),main = Meginio_Vieta, panel.first = grid(nx = 10, ny = 10))
axis(1, at=c(1,3,5,7,9,11))
mtext("Vasaros sezonas",cex = 0.8,adj=0.5, padj=0)
lines(mydensity,col="red", lwd=2)

myhist <- hist(Data_3,breaks=DataBins, plot=FALSE)
multiplier <- myhist$counts / myhist$density
mydensity <- Data_3_density
mydensity$y <- mydensity$y * multiplier[1]
plot(myhist,xlim =c(0,11),col=Col_2,xlab = expression(NH[4]*-N~ "koncentracija mg/l"),main = Meginio_Vieta, panel.first = grid(nx = 10, ny = 10))
axis(1, at=c(1,3,5,7,9,11))
mtext("Rudens sezonas",cex = 0.8,adj=0.5, padj=0)
lines(mydensity,col="red", lwd=2)

myhist <- hist(Data_4,breaks=DataBins, plot=FALSE)
multiplier <- myhist$counts / myhist$density
mydensity <- Data_4_density
mydensity$y <- mydensity$y * multiplier[1]
plot(myhist,xlim =c(0,11),col=Col_2,xlab = expression(NH[4]*-N~ "koncentracijamg/l"),main = Meginio_Vieta, panel.first = grid(nx = 10, ny = 10))
axis(1, at=c(1,3,5,7,9,11))
mtext("Žiemos sezonas",cex = 0.8,adj=0.5, padj=0)
lines(mydensity,col="red", lwd=2)

`

Data:

Sul11P Sul11V Sul11R Sul11Z

2,7699 1,4872 0,0725 1,2158

4,9356 3,6808 1,1823 9,5106

0,6507 0,5205 1,3292

1,2734 1,7568 0,1431

1,7568 1,2251 0,541 4,3036

0,3183 1,0875 1,2158 4,0489

1,5058 1,3571 1,4593 0,0279

0,158 0,8682 0,4982 1,0132

9,3601 0,2733 0,9983 1,2511

3,0971 0,5707 1,2232 0,593

4,4951 0,0409 1,1247 0,2175

4,311 0,1208 2,11 3,9392

5,7146 0,0502 1,5307 5,9153

4,3408 1,9105 1,3534 2,4105

1,279 2,3093 1,9105 1,3133

4,2088 1,1108 0,6023 1,2585

2,6863 2,3662 2,2185 1,1952

1,6099 4,5 1,0791 2,1151

1,3998 2,3916 1,4188 1,8873

1,292 3,0035 0,202 1,9337

1,314 2,7165 0,9568

1,56 0,3997 1,0116

1,3049  2,2101  

1,7164  0,2647  

    3,225   

It seems to plot well, result: http://i60.tinypic.com/2ewzh2u.jpg

But when I try to plot this data:

Sul13P Sul13V Sul13R Sul13Z 6,0046 6,5995 0,5242 8,5514 6,4228 7,5475 2,307 8,9529 4,1177 6,4136 2,4543
4,7683 7,225 1,8367 1,6025 4,6289 2,3247 1,6303 2,4985 0,1573 2,8647 1,4946 1,2195 4,8985 3,8333 2,268 3,2923 5,6235 4,034 0,5763 3,1789 10,8008 4,6345 2,5171 1,833 4,5062 4,8334 2,1304 1,8348 3,7998 3,7775 1,1805 0,4294 2,6175 2,8108 1,6824 3,7217 2,861 2,0003 5,5207 3,5823 4,8037 2,8262 4,1555 2,0624 2,6007 4,548 5,7021 2,4506 4,7572 1,0179 3,4402 1,8176 4,0582 4,8054 5,6494 1,5855 2,5487 3 6,16 2,3472 3,6195 3,3706 6,466 1,518 2,7272 2,9802 4,8413 2,324 2,258 5,9216 3,2208 2,9591 2,6914 5,4954 2,765 2,8241 NA 4,6303 2,5414 1,6298 NA 5,7338 0,4271 2,2164 NA NA 2,8515 1,5328
I get this result:http://i61.tinypic.com/1z6quqg.jpg. For some reason one density curve is not plotted. This happens with other similar data sets I have.

Whats is wrong with my code?

user3434943
  • 21
  • 2
  • 5
  • I think because `multiplier[1] == 0`. If `myhist$counts[1]` or `myhist$density[1]` is 0, your multiplier is 0 or inf. – koekenbakker Mar 24 '14 at 20:47
  • Do you know how to fix this isue? – user3434943 Mar 24 '14 at 20:55
  • I checked multiplier and it prints lines like this: 7.5 NaN 7.5 7.5 7.5 7.5 7.5 7.5 7.5 NaN 7.5 NaN NaN NaN N NaN NaN 12 12 12 12 12 NaN 12 12 NaN NaN 12 NaN NaN 12.5 12.5 12.5 12.5 12.5 12.5 NaN NaN NaN NaN NaN NaN 5.5 5.5 5.5 5.5 NaN NaN NaN NaN NaN NaN NaN NaN NaN. When first value is Nan then density curve is not plotted. So maybe is way how to remove these NaN values? – user3434943 Mar 25 '14 at 09:23
  • Exactly, so instead of `multiplier[1]` use `multiplier[!is.na(multiplier)][1]` – koekenbakker Mar 25 '14 at 09:28
  • Thank You very much, now it seems to work very well. I used this solution proposed from here to get multiplier: [link]http://stackoverflow.com/questions/20078107/overlay-normal-curve-to-histogram-in-r which was for data without Na values. – user3434943 Mar 25 '14 at 09:38

0 Answers0