[R] x tick labels - sparse?

Darren Weber darrenleeweber at gmail.com
Sat Oct 21 04:04:33 CEST 2006


OK, so that works.  It reduces the number of tick marks and labels by
reduction of the x array to elements that are multiples of 100.  That
is nice, but it's not what I really want.

I do want tick marks at all elements of x, but only a sparse number of
tick labels.  I want to control what the tick labels are.  I have
trouble with this:

x <- seq(-100,1000,25)
y <- x * x
plot(x, y, xaxt = "n")
axis(1, x, labels = as.character( x[x %% 100 == 0] ) )

Error in axis(side, at, labels, tick, line, pos, outer, font, vfont, lty,  :
	'at' and 'label' lengths differ, 45 != 12

I've tried to match the label lengths using blanks and this will
generate a plot with tick marks at all elements of x, but the x tick
labels fail after plotting the first or second label.  There is no
error or warning.  Try this and see if you can replicate the bug that
I observe and advise me how to get around it:

x <- seq(-100,1000,25)
y <- x * x
plot(x, y, xaxt = "n")
xtickLabels <- as.character(x)
for (i in 1:length(x) ){
    if ((i %% 2) == 0 ) xtickLabels[i] <- ' '
} # BTW, could this syntax be more succint?
axis(1, x, labels=xtickLabels)


I see only the first tick label (-100) and nothing else.

Thanks, Darren





On 8/11/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Try this:
>
> x <- seq(-100,1000,25)
> y <- x * x
> plot(x, y, xaxt = "n")
> axis(1, x[x %% 100 == 0])
>
>
>
> On 8/11/06, Darren Weber <darrenleeweber at gmail.com> wrote:
> > Hi,
> >
> > I'm stuck on creating a plot with x tick labels for every Nth tick
> > mark - how is that done?  I don't see a simple solution to this in
> > help(plot) or help(par) and what I've tried is not working, eg, the
> > following does not work, although it seems intuitive to me that it
> > should work:
> >
> > x <- seq(-100,1000,25)
> > y <- x * x
> > % find all the x values that are multiples of 100
> > tmp <- x / 100
> > tmp <- tmp %% 1
> > tmp <- tmp > 0
> > % set all other values to null strings
> > xtickLabels <- as.character( x )
> > xtickLabels[tmp] <- ""
> > plot(x, y, xlab=xtickLabels)
> >
> >
> > These commands look like this (the plot is not right):
> >
> > > x <- seq(-100,1000,25)
> > > x
> >  [1] -100  -75  -50  -25    0   25   50   75  100  125  150  175  200  225  250
> > [16]  275  300  325  350  375  400  425  450  475  500  525  550  575  600  625
> > [31]  650  675  700  725  750  775  800  825  850  875  900  925  950  975 1000
> > >
> > > tmp <- x / 100
> > > tmp
> >  [1] -1.00 -0.75 -0.50 -0.25  0.00  0.25  0.50  0.75  1.00  1.25  1.50  1.75
> > [13]  2.00  2.25  2.50  2.75  3.00  3.25  3.50  3.75  4.00  4.25  4.50  4.75
> > [25]  5.00  5.25  5.50  5.75  6.00  6.25  6.50  6.75  7.00  7.25  7.50  7.75
> > [37]  8.00  8.25  8.50  8.75  9.00  9.25  9.50  9.75 10.00
> > >
> > > tmp <- tmp %% 1
> > > tmp
> >  [1] 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50
> > [16] 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25
> > [31] 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00 0.25 0.50 0.75 0.00
> > >
> > > tmp <- tmp > 0
> > > tmp
> >  [1] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
> > [13] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
> > [25] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
> > [37] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
> > >
> > > xtickLabels <- as.character( x )
> > > xtickLabels
> >  [1] "-100" "-75"  "-50"  "-25"  "0"    "25"   "50"   "75"   "100"  "125"
> > [11] "150"  "175"  "200"  "225"  "250"  "275"  "300"  "325"  "350"  "375"
> > [21] "400"  "425"  "450"  "475"  "500"  "525"  "550"  "575"  "600"  "625"
> > [31] "650"  "675"  "700"  "725"  "750"  "775"  "800"  "825"  "850"  "875"
> > [41] "900"  "925"  "950"  "975"  "1000"
> > >
> > > xtickLabels[tmp] <- ""
> > > xtickLabels
> >  [1] "-100" ""     ""     ""     "0"    ""     ""     ""     "100"  ""
> > [11] ""     ""     "200"  ""     ""     ""     "300"  ""     ""     ""
> > [21] "400"  ""     ""     ""     "500"  ""     ""     ""     "600"  ""
> > [31] ""     ""     "700"  ""     ""     ""     "800"  ""     ""     ""
> > [41] "900"  ""     ""     ""     "1000"
> > >
> > > y <- x * x
> > > y
> >  [1]   10000    5625    2500     625       0     625    2500    5625   10000
> > [10]   15625   22500   30625   40000   50625   62500   75625   90000  105625
> > [19]  122500  140625  160000  180625  202500  225625  250000  275625  302500
> > [28]  330625  360000  390625  422500  455625  490000  525625  562500  600625
> > [37]  640000  680625  722500  765625  810000  855625  902500  950625 1000000
> > >
> > > plot(x, y, xlab=xtickLabels)
> > >
> >
> >
> > Thanks in advance.
> >
> > Best, Darren
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch 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.
> >
>



More information about the R-help mailing list