[R] separate elements of a character vector

Joshua Wiley jwiley.psych at gmail.com
Tue Oct 19 19:06:24 CEST 2010


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/



More information about the R-help mailing list