0

I was trying to fit 2 curves in one plot. One graph contains a histogram and the same graph contain the normal curve and lastly, the kernel function. However, I can't get more than one graph to show on the plot, here is what I am doing:

hist(index$lognr, breaks = 100, prob=T)
mean_DJ = mean(index$lognr, na.rm=T);
sd_DJ = sd(index$lognr, na.rm=T);
//For the normal curve
curve(dnorm(x, mean_DJ, sd_DJ), add=T, col = "grey", lwd = 2)
//For kernel density
lines(density(index$lognr, na.rm=T), col = "blue", lwd = 2)



 //30 sample data pointsstructure(list(Date = structure(c(1398L, 1381L, 1376L, 1372L, 
    > structure(head(index_A, 30))
        Date  Close        LogNR
1  9/24/2012 690.79 -0.013373028
2  9/21/2012 700.09  0.001987433
3  9/20/2012 698.70 -0.004854378
4  9/19/2012 702.10  0.000270653
5  9/18/2012 701.91  0.003039191
6  9/17/2012 699.78  0.012221048
7  9/14/2012 691.28  0.012079375
8  9/13/2012 682.98  0.019501346
9  9/12/2012 669.79  0.013830855
10 9/11/2012 660.59 -0.003249381
11 9/10/2012 662.74 -0.026356891
12  9/7/2012 680.44  0.006147243
13  9/6/2012 676.27  0.008971468
14  9/5/2012 670.23 -0.007047308
15  9/4/2012 674.97  0.014520368
16 8/31/2012 665.24  0.002061531
17 8/30/2012 663.87 -0.014357104
18 8/29/2012 673.47 -0.001972899
19 8/28/2012 674.80 -0.001303241
20 8/27/2012 675.68  0.018612831
21 8/24/2012 663.22  0.000889995
22 8/23/2012 662.63 -0.009372956
23 8/22/2012 668.87  0.019337473
24 8/21/2012 656.06 -0.013760331
25 8/20/2012 665.15  0.025952144
26 8/17/2012 648.11  0.018327423
27 8/16/2012 636.34  0.008696599
28 8/15/2012 630.83 -0.001362355
29 8/14/2012 631.69  0.002678948
30 8/13/2012 630.00  0.013262158
Josh
  • 3,231
  • 8
  • 37
  • 58
  • Can you make your question [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? I don't have your data, but your code works with some random data I made up: `x <- rnorm(100,4)` – Chase Sep 29 '12 at 01:32
  • Would I need to provide my data? – Josh Sep 29 '12 at 01:36
  • Either that or fake data that reproduces the behavior you're seeing. – joran Sep 29 '12 at 01:42
  • The data is closing prices and returns of Apple stocks from Jan 1st 2007 to Sep 2012. So there are about 1400 data points. I have tried dput() but I'm not sure of what to do after – Josh Sep 29 '12 at 01:44
  • 1400 is probably overkill, but maybe you can reproduce the problem with 20-30 data points? Maybe try pasting the results of `dput(head(yourData,30))` into your question. If that doesn't reproduce your problem...that gives you a good indication of where the problem may lie. – Chase Sep 29 '12 at 01:46
  • I put up about 30 points of data – Josh Sep 29 '12 at 01:51
  • No, no, no. Post the entire structure( head(object, 30) ) output. Otherwise there is no way to make a valid R object. – IRTFM Sep 29 '12 at 02:19
  • A cut-down version of this works fine for me. I assume you're not ignoring case-sensitivity (`LogNR` vs `lognr`), otherwise you would be getting obvious error messages (which I hope you would share with us). The only other thing I can think of is the `T` vs `TRUE` trap; do you have an object called `T` defined in your workspace? What happens if you substitue `TRUE` for `T` above? – Ben Bolker Sep 29 '12 at 02:21
  • T or TRUE, it gives the same result. There is a message after the graph: In curve(...) 'add' will be ignored as there is no existing plot. Which is weird because I plot the histogram too. – Josh Sep 29 '12 at 02:40
  • I have also updated the question with the full structure output! – Josh Sep 29 '12 at 02:43
  • did you try `hist(index$LogNR, breaks = 100, prob=TRUE)` (note case of `LogNR`) ? Also, I note that you refer to both `index` and `index_A` above. Be careful! – Ben Bolker Sep 29 '12 at 02:51
  • In the example above, I'm using index, but in the real program I'm using index_A. I'll try it on a windows OS, maybe that will make a difference – Josh Sep 29 '12 at 13:17

1 Answers1

2

I think your problem lies with not paying close attention to spelling:

hist(index$LogNR, breaks = 100, prob=T)
mean_DJ = mean(index$LogNR, na.rm=T);
sd_DJ = sd(index$LogNR, na.rm=T);
#For the normal curve
curve(dnorm(x, mean_DJ, sd_DJ), add=T, col = "grey", lwd = 2)
#For kernel density
lines(density(index$LogNR, na.rm=T), col = "blue", lwd = 2)

enter image description here

index <- read.table(text="      Date  Close        LogNR
1  9/24/2012 690.79 -0.013373028
2  9/21/2012 700.09  0.001987433
3  9/20/2012 698.70 -0.004854378
4  9/19/2012 702.10  0.000270653
5  9/18/2012 701.91  0.003039191
6  9/17/2012 699.78  0.012221048
7  9/14/2012 691.28  0.012079375
8  9/13/2012 682.98  0.019501346
9  9/12/2012 669.79  0.013830855
10 9/11/2012 660.59 -0.003249381
11 9/10/2012 662.74 -0.026356891
12  9/7/2012 680.44  0.006147243
13  9/6/2012 676.27  0.008971468
14  9/5/2012 670.23 -0.007047308
15  9/4/2012 674.97  0.014520368
16 8/31/2012 665.24  0.002061531
17 8/30/2012 663.87 -0.014357104
18 8/29/2012 673.47 -0.001972899
19 8/28/2012 674.80 -0.001303241
20 8/27/2012 675.68  0.018612831
21 8/24/2012 663.22  0.000889995
22 8/23/2012 662.63 -0.009372956
23 8/22/2012 668.87  0.019337473
24 8/21/2012 656.06 -0.013760331
25 8/20/2012 665.15  0.025952144
26 8/17/2012 648.11  0.018327423
27 8/16/2012 636.34  0.008696599
28 8/15/2012 630.83 -0.001362355
29 8/14/2012 631.69  0.002678948
30 8/13/2012 630.00  0.013262158
", header=TRUE)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • I put in the correct spelling, but I still can't see a graph like yours above. Does it have anything to do with Ubuntu? – Josh Sep 29 '12 at 13:15
  • It seems unlikely that it has anything to do with Ubuntu. More likely you do not understand the data structure you have created. I will stick the code I used to process your data below the graph. – IRTFM Sep 29 '12 at 15:09
  • I'm not sure what is going wrong, when I plot the histogram, the correct graph shows up, but when I try it with the normal/kernel plots, it doesn't work – Josh Sep 29 '12 at 17:34
  • I don't know where you are going wrong either ... I'm on a Mac and I just repeated this with an X11() graphics device and get the same results (up to the more limited resolution of that device.) Maybe other Linux users need to try. Still seems unlikely that this should be failing on Ubuntu, since it is very generic code. Are you using my data or your data? – IRTFM Sep 29 '12 at 18:32
  • I was using my own data. I changed the file around and everything. The problem is that I have about 1500 data points for a company, and I have about 15 companies that I want to plot/ do other statistical analysis to. So I have to figure out what I'm doing wrong. – Josh Sep 29 '12 at 19:45
  • It's always a good idea to post str(dataobject). That is usually (although not always) a fairly compact outline. – IRTFM Sep 29 '12 at 20:19