Dear colleagues, this seems like an easy problem, and I found some suggestions which I've incorporated in the help list, but I can't quite get it right. I want to add a series of years to a second x-axis category label. I generate them with test and test_2 below, format them with some spacing (which is the suggestion I took from the R-list) and concatenate them and then write them with mtext. At the end, the labels in test are bunched up together in the center of the plot window. Can anyone suggest a way to space out the elements of "test" to look like evenly-spaced x-labels? Yours, Simon Kiss x1<-rnorm(500) plot(x1) test<-seq(1987, 2002, by=1) test_2<-seq(2003, 2006, by=1) test<-format(c(test, test_2), width=5) mtext(test, side=1, line=2) ********************************* Simon J. Kiss, PhD Assistant Professor, Wilfrid Laurier University 73 George Street Brantford, Ontario, Canada N3T 2C9 Cell: +1 519 761 7606
Dear Simon, I think the main issue is that mtext() is designed to work with a single character string, not a character vector. Here is one approach collapsing using paste with some space: x1<-rnorm(500) plot(x1) test<-seq(1987, 2002, by=1) test_2<-seq(2003, 2006, by=1) mtext(paste(c(test, test_2), collapse = " "), side=1, line=2) That said, if you want to add years as an alternate axis, mtext() is not the route to go. If you really want years as an axis, they should be aligned with x values, not guessed spacing. I can provide examples if that is in fact what you are after. Cheers, Josh On Tue, Oct 19, 2010 at 9:55 AM, Simon Kiss <sjkiss at gmail.com> wrote:> Dear colleagues, this seems like an easy problem, and I found some suggestions which I've incorporated in the help list, but I can't quite get it right. > I want to add a series of years to a second x-axis category label. I generate them with test and test_2 below, format them with some spacing (which is the suggestion I took from the R-list) and concatenate them and then write them with mtext. ?At the end, the labels in test are bunched up together in the center of the plot window. ?Can anyone suggest a way to space out the elements of "test" to look like evenly-spaced x-labels? > Yours, > Simon Kiss > > x1<-rnorm(500) > plot(x1) > > test<-seq(1987, 2002, by=1) > > test_2<-seq(2003, 2006, by=1) > > test<-format(c(test, test_2), width=5) > > mtext(test, side=1, line=2) > ********************************* > Simon J. Kiss, PhD > Assistant Professor, Wilfrid Laurier University > 73 George Street > Brantford, Ontario, Canada > N3T 2C9 > Cell: +1 519 761 7606 > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
You may want to try something like this: x1<-rnorm(500) plot(x1) test<-seq(1987, 2002, by=1) test_2<-seq(2003, 2006, by=1) test<-format(c(test, test_2), width=5) xxx<-seq(1,500,length=length(test)) axis(1,at=xxx,labels=test,line=1,col=0) You'll need to specify where you want the labels (in this code I do that with xxx). -tgs On Tue, Oct 19, 2010 at 12:55 PM, Simon Kiss <sjkiss@gmail.com> wrote:> Dear colleagues, this seems like an easy problem, and I found some > suggestions which I've incorporated in the help list, but I can't quite get > it right. > I want to add a series of years to a second x-axis category label. I > generate them with test and test_2 below, format them with some spacing > (which is the suggestion I took from the R-list) and concatenate them and > then write them with mtext. At the end, the labels in test are bunched up > together in the center of the plot window. Can anyone suggest a way to > space out the elements of "test" to look like evenly-spaced x-labels? > Yours, > Simon Kiss > > x1<-rnorm(500) > plot(x1) > > test<-seq(1987, 2002, by=1) > > test_2<-seq(2003, 2006, by=1) > > test<-format(c(test, test_2), width=5) > > mtext(test, side=1, line=2) > ********************************* > Simon J. Kiss, PhD > Assistant Professor, Wilfrid Laurier University > 73 George Street > Brantford, Ontario, Canada > N3T 2C9 > Cell: +1 519 761 7606 > > ______________________________________________ > 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]]
Maybe Matching Threads
- using lapply with recode
- Create single vector after looping through multiple data frames with GREP
- cycling from x11 window in RCommander to graphics device window: Mac Os 10.6.8
- Changing line length in Sweave output works for numeric, but not for character vectors
- Listing tables together from random samples from a generated population?