Cech, Christian
2013-Sep-09 16:00 UTC
[R] Problem adding lines to a plot, when y is defined
Dear all, I want to create a line-plot with two lines and some additional scatter-plots. However, adding a line to the plot does not work when I specify the y-argument in the plot command, while it does work when y is not specified. I first send you an example that works: plot(var[, 3], type="l", ylim=c(-0.04, 0), ylab = 'portfolio returns', xlab = 'time') lines(var[, 5], type="l", lty=3) for (i in 1:nrow(var)) { if(var[i, 4] | var[i,6]) points(i, var[i, 2], pch=4) } --> the result is displayed in attachment "Plot1.pdf" What does not work is the following code, where as the first argument of plot (ie the y-argument) is defined: plot(as.Date(var[, 1], origin="1899-12-30"), var[, 3], type="l", ylim=c(-0.04, 0), ylab='portfolio returns', xlab='time') lines(var[, 5], type="l", lty=3) for (i in 1:nrow(var)) { if(var[i, 4] | var[i,6]) points(i, var[i, 2], pch=4) } --> the result is displayed in attachment "Plot2.pdf" The same problem appears if instead of as.Date(var[, 1], origin="1899-12-30") I use var[, 1] to define the y-argument. I do very much appreciate your help! Kind regards, Christian Cech ________________________________ Firmenwortlaut: Fachhochschule des bfi Wien Gesellschaft m.b.H Firmenbuchnummer: 148597 a Firmenbuchgericht: Handelsgericht Wien Firmensitz: Wohlmutstra?e 22, 1020 Wien This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required please request a hard-copy version. -------------- next part -------------- A non-text attachment was scrubbed... Name: Plot1.pdf Type: application/pdf Size: 170140 bytes Desc: Plot1.pdf URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130909/424ca860/attachment.pdf> -------------- next part -------------- A non-text attachment was scrubbed... Name: Plot2.pdf Type: application/pdf Size: 87948 bytes Desc: Plot2.pdf URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130909/424ca860/attachment-0001.pdf>
David Winsemius
2013-Sep-09 17:57 UTC
[R] Problem adding lines to a plot, when y is defined
On Sep 9, 2013, at 9:00 AM, Cech, Christian wrote:> Dear all, > > I want to create a line-plot with two lines and some additional scatter-plots. > However, adding a line to the plot does not work when I specify the y-argument in the plot command, while it does work when y is not specified. > > I first send you an example that works: > plot(var[, 3], > type="l", > ylim=c(-0.04, 0), > ylab = 'portfolio returns', > xlab = 'time') > lines(var[, 5], type="l", lty=3) > for (i in 1:nrow(var)) { > if(var[i, 4] | var[i,6]) > points(i, var[i, 2], pch=4) > } > > --> the result is displayed in attachment "Plot1.pdf" > > What does not work is the following code, where as the first argument of plot (ie the y-argument) is defined: > plot(as.Date(var[, 1], origin="1899-12-30"), > var[, 3], > type="l", > ylim=c(-0.04, 0), > ylab='portfolio returns', > xlab='time') > lines(var[, 5], type="l", lty=3) > for (i in 1:nrow(var)) { > if(var[i, 4] | var[i,6]) > points(i, var[i, 2], pch=4) > } > > --> the result is displayed in attachment "Plot2.pdf" > > The same problem appears if instead of > > as.Date(var[, 1], origin="1899-12-30") > > I use > > var[, 1] > > to define the y-argument.This puzzles me. At the moment yu are using postional matching and it is var[,3] that would be matched to the y argument. You need to post output of str(var) so that we can see the class of var[,1].>-- David Winsemius Alameda, CA, USA
Cech, Christian
2013-Sep-09 19:27 UTC
[R] Problem adding lines to a plot, when y is defined
>> On Sep 9, 2013, at 9:00 AM, Cech, Christian wrote: >> >>> Dear all, >>> >>> I want to create a line-plot with two lines and some additional scatter-plots. >>> However, adding a line to the plot does not work when I specify the y-argument in the plot command, while it does work when y is not specified. >>> >>> I first send you an example that works: >>> plot(var[, 3], >>> type="l", >>> ylim=c(-0.04, 0), >>> ylab = 'portfolio returns', >>> xlab = 'time') >>> lines(var[, 5], type="l", lty=3) >>> for (i in 1:nrow(var)) { >>> if(var[i, 4] | var[i,6]) >>> points(i, var[i, 2], pch=4) >>> } >>> >>> --> the result is displayed in attachment "Plot1.pdf" >>> >>> What does not work is the following code, where as the first argument of plot (ie the y-argument) is defined: >>> plot(as.Date(var[, 1], origin="1899-12-30"), >>> var[, 3], >>> type="l", >>> ylim=c(-0.04, 0), >>> ylab='portfolio returns', >>> xlab='time') >>> lines(var[, 5], type="l", lty=3) >>> for (i in 1:nrow(var)) { >>> if(var[i, 4] | var[i,6]) >>> points(i, var[i, 2], pch=4) >>> } >>> >>> --> the result is displayed in attachment "Plot2.pdf" >>> >>> The same problem appears if instead of >>> >>> as.Date(var[, 1], origin="1899-12-30") >>> >>> I use >>> >>> var[, 1] >>> >>> to define the y-argument. >> >> This puzzles me. At the moment yu are using postional matching and it is var[,3] that would be matched to the y argument. >> >> You need to post output of str(var) so that we can see the class of var[,1]. >> >>> Dear David, > > thank you for the quick reply! Unfortunately I confused you because I mixed up y-value and x-value. What I actually wanted to say is that the plot does not work when x is specified (2nd example) while it does work if x is not specified (1st example). Sorry for this! > y = var[, 3] > x = var[, 1] > > When I type str(var) i get the following output: > num [1:4747, 1:6] 33450 33451 33452 33455 33456 ... > > Is that the information you needed?Maybe. 'var' is a numeric matrix which may not be what you think it was if it started out life as having dates. (On this list it is requested that you post in context which means bottom posting in many cases. So I moved your reply.) When you apply the as.Date function with an origin, you will get an different number as the internal representation of the Date value than what you put in: as.numeric(as.Date(c(33450, 33451, 33452, 33455, 33456), origin="1899-01-01")) [1] 7518 7519 7520 7523 7524 I suspect that you have a misregistration of the x values. Since it is a matrix we might have better view of it with: dput(head(var)) I also supect that you will end up using the `axis` function to do your labeling. -- David Winsemius Alameda, CA, USA > dput(head(var)) structure(c(33450, 33451, 33452, 33455, 33456, 33457, -0.00299369609998333, -0.000882805884818571, -0.000570821462061429, 0.00735101809395667, -0.000574642535598095, -0.000464474459045714, -0.010510137214196, -0.0105173750100507, -0.0104987866171353, -0.0104808796029616, -0.0104561013745608, -0.0104562836793838, 0, 0, 0, 0, 0, 0, -0.0115901618181903, -0.0115901618181903, -0.0115901618181903, -0.0115901618181903, -0.0115901618181903, -0.0115901618181903, 0, 0, 0, 0, 0, 0), .Dim = c(6L, 6L), .Dimnames = list(NULL, c("col1", "col2", "col3", "col4", "col5", "col6"))) Christian ________________________________ Firmenwortlaut: Fachhochschule des bfi Wien Gesellschaft m.b.H Firmenbuchnummer: 148597 a Firmenbuchgericht: Handelsgericht Wien Firmensitz: Wohlmutstra?e 22, 1020 Wien This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. If verification is required please request a hard-copy version.
Enrico Schumann
2013-Sep-09 20:00 UTC
[R] Problem adding lines to a plot, when y is defined
On Mon, 09 Sep 2013, "Cech, Christian" <Christian.Cech at fh-vie.ac.at> writes:> Dear all, > > I want to create a line-plot with two lines and some additional scatter-plots. > However, adding a line to the plot does not work when I specify the y-argument in the plot command, while it does work when y is not specified. > > I first send you an example that works: > plot(var[, 3], > type="l", > ylim=c(-0.04, 0), > ylab = 'portfolio returns', > xlab = 'time') > lines(var[, 5], type="l", lty=3) > for (i in 1:nrow(var)) { > if(var[i, 4] | var[i,6]) > points(i, var[i, 2], pch=4) > } > > --> the result is displayed in attachment "Plot1.pdf" > > What does not work is the following code, where as the first argument of plot (ie the y-argument) is defined: > plot(as.Date(var[, 1], origin="1899-12-30"), > var[, 3], > type="l", > ylim=c(-0.04, 0), > ylab='portfolio returns', > xlab='time') > lines(var[, 5], type="l", lty=3) > for (i in 1:nrow(var)) { > if(var[i, 4] | var[i,6]) > points(i, var[i, 2], pch=4) > } > > --> the result is displayed in attachment "Plot2.pdf" > > The same problem appears if instead of > > as.Date(var[, 1], origin="1899-12-30") > > I use > > var[, 1] > > to define the y-argument.Hard to say without a reproducible example, but try lines(as.Date(var[, 1], origin="1899-12-30"), var[, 5]) instead.> > I do very much appreciate your help! > Kind regards, > Christian Cech >[...] -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net
In the second example, in the three functions that create the plot: plot() lines() points() all must have two arguments, the first of which is the x-values (dates), and the second of which is the y-values associated with the dates. For example, in your second version, where you have points(i, var[i, 2], pch=4) you want to replace i with the date associated with the i'th row in your data. You may also be slightly confused about which argument to plot() is the "y-argument". In your second example, the y-argument is the second argument, not the first. Compare the results of these two commands: plot( c(5,3,6) ) plot( c(2,9,10) , c(5,3,6) ) -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 9/9/13 9:00 AM, "Cech, Christian" <Christian.Cech at fh-vie.ac.at> wrote:>Dear all, > >I want to create a line-plot with two lines and some additional >scatter-plots. >However, adding a line to the plot does not work when I specify the >y-argument in the plot command, while it does work when y is not >specified. > >I first send you an example that works: >plot(var[, 3], > type="l", > ylim=c(-0.04, 0), > ylab = 'portfolio returns', > xlab = 'time') >lines(var[, 5], type="l", lty=3) >for (i in 1:nrow(var)) { > if(var[i, 4] | var[i,6]) > points(i, var[i, 2], pch=4) >} > >--> the result is displayed in attachment "Plot1.pdf" > >What does not work is the following code, where as the first argument of >plot (ie the y-argument) is defined: >plot(as.Date(var[, 1], origin="1899-12-30"), > var[, 3], > type="l", > ylim=c(-0.04, 0), > ylab='portfolio returns', > xlab='time') >lines(var[, 5], type="l", lty=3) >for (i in 1:nrow(var)) { > if(var[i, 4] | var[i,6]) > points(i, var[i, 2], pch=4) >} > >--> the result is displayed in attachment "Plot2.pdf" > >The same problem appears if instead of > >as.Date(var[, 1], origin="1899-12-30") > >I use > >var[, 1] > >to define the y-argument. > >I do very much appreciate your help! >Kind regards, >Christian Cech > >________________________________ > > >Firmenwortlaut: Fachhochschule des bfi Wien Gesellschaft m.b.H >Firmenbuchnummer: 148597 a >Firmenbuchgericht: Handelsgericht Wien >Firmensitz: Wohlmutstra?e 22, 1020 Wien > >This message contains confidential information and is i...{{dropped:12}}