Dear all, I'm attempting to insert a legend into a line graph. I've sorted out the positioning, but I'm unable to display the sample line and associated colour to go within the legend box. Instead, under the variable names, the numbers 1, 2, 2, 3 are displayed in a column (with '2' repeated twice). This is the code I'm using: legend(80,1150, c("Simulation", "Observation", lty=1:2, col=2:3) How do I go about displaying a red solid line next to 'Simulation' and a green dashed line next to 'Observation' (and if necessary, remove the numbers that are currently displayed)? Many thanks for any help offered, Steve
Steve Murray <smurray444 at hotmail.com> wrote> >I'm attempting to insert a legend into a line graph. I've sorted out the positioning, but I'm unable to display the sample line and associated colour to go within the legend box. Instead, under the variable names, the numbers 1, 2, 2, 3 are displayed in a column (with '2' repeated twice). This is the code I'm using: > >legend(80,1150, c("Simulation", "Observation", lty=1:2, col=2:3) >You've got mismatched parentheses here, with two ( and only one ). I suspect you added one at the end, making r look at lty= and col = as part of the legend text. You want legend(80,1150, c("Simulation", "Observation"), lty=1:2, col=2:3) HTH Peter Peter L. Flom, PhD Statistical Consultant www DOT peterflomconsulting DOT com
This works for me:> plot(1:10, 1:10) > legend("topleft", c("Simulation", "Observation"), lty=1:2, col=2:3)Try that; if it does work, then we need more information about exactly what you're doing, because there's a problem not contained in just that single line of code (which has a problem of its own!). Sarah On Mon, May 4, 2009 at 2:46 PM, Steve Murray <smurray444 at hotmail.com> wrote:> > Dear all, > > I'm attempting to insert a legend into a line graph. I've sorted out the positioning, but I'm unable to display the sample line and associated colour to go within the legend box. Instead, under the variable names, the numbers 1, 2, 2, 3 are displayed in a column (with '2' repeated twice). This is the code I'm using: > > legend(80,1150, c("Simulation", "Observation", lty=1:2, col=2:3) > > How do I go about displaying a red solid line next to 'Simulation' and a green dashed line next to 'Observation' (and if necessary, remove the numbers that are currently displayed)? >-- Sarah Goslee http://www.functionaldiversity.org
Dear Steve, Try this: x<-rnorm(100) y<-rnorm(100) + 2*x plot(x,y) legend('topleft', c("Simulation", "Observation"), lty=1:2, col=2:3) abline(x,y,col=3,lty=2) (you forgot a ")" in your code :( ) HTH, Jorge On Mon, May 4, 2009 at 2:46 PM, Steve Murray <smurray444@hotmail.com> wrote:> > Dear all, > > I'm attempting to insert a legend into a line graph. I've sorted out the > positioning, but I'm unable to display the sample line and associated colour > to go within the legend box. Instead, under the variable names, the numbers > 1, 2, 2, 3 are displayed in a column (with '2' repeated twice). This is the > code I'm using: > > legend(80,1150, c("Simulation", "Observation", lty=1:2, col=2:3) > > How do I go about displaying a red solid line next to 'Simulation' and a > green dashed line next to 'Observation' (and if necessary, remove the > numbers that are currently displayed)? > > Many thanks for any help offered, > > Steve > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
your code is missing a closing bracket for the text labels, legend(1,4, c("Simulation", "Observation"), lty=1:2, col=2:3) baptiste On 4 May 2009, at 20:46, Steve Murray wrote:> > Dear all, > > I'm attempting to insert a legend into a line graph. I've sorted out > the positioning, but I'm unable to display the sample line and > associated colour to go within the legend box. Instead, under the > variable names, the numbers 1, 2, 2, 3 are displayed in a column > (with '2' repeated twice). This is the code I'm using: > > legend(80,1150, c("Simulation", "Observation", lty=1:2, col=2:3) > > How do I go about displaying a red solid line next to 'Simulation' > and a green dashed line next to 'Observation' (and if necessary, > remove the numbers that are currently displayed)? > > Many thanks for any help offered, > > Steve > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag