[R] Matrices

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Aug 10 20:15:23 CEST 2009


On Mon, 2009-08-10 at 14:02 -0400, mmv.listservs wrote:
> Your example works because you have only 5 really long labels. I tried
> changing 5 to 474 because that is how many I have and the plot looks
> illegible. Should I increase the margin or the height of the plotting
> window? Thanks for your help again.

It worked, for some definition of "worked". Not mine though. Notice how
in both your and my examples, the first part of the text label is
truncated, off to the left of the plot. In your example, the extra
complication is that you have so many text labels, so we get overlap of
labels *and* the truncation. My solution (in earlier posts) only helps
to fix the latter. To fix the overlapping labels, you could do one of
two things:

1) Increase the height of the plot window or the device on which you are
plotting.
2) Decrease the size of the size of the font used to draw the tick
labels

If we extend your modification of my example (I reduce the number of
levels as 474 is too many).

## dummy data
set.seed(123)
dummy <- data.frame(A = sample(paste("really long labels",1:100), 10000,
                              replace = TRUE),
                   B = rnorm(10000))

## We can then try this:

op <- par(mar = c(5,6,4,2) + 0.1)
boxplot(B ~ A, data = dummy, horizontal = TRUE, axes = FALSE)
axis(side = 1)
axis(side = 2, at = seq_along(levels(dummy$A)),
     labels = levels(dummy$A), cex.axis = 0.5,
     las = 1)
box()
par(op)

But trying to show 474 levels on a plot like this and have the label
readable is asking quite a lot.

HTH

G

> 
> ## dummy data
> set.seed(123)
> dummy <- data.frame(A = sample(paste("really long labels", 1:474),
> 100,
>                               replace = TRUE),
>                    B = rnorm(100))
> str(dummy)
> head(dummy)
> ## plot it
> boxplot(B ~ A, data = dummy, horizontal = TRUE, las = 1)
> ## Hmm labels are truncated on left margin. How do I stop that?
> 
> 
> On Mon, Aug 10, 2009 at 1:57 PM, Gavin Simpson
> <gavin.simpson at ucl.ac.uk> wrote:
>         On Mon, 2009-08-10 at 13:41 -0400, mmv.listservs wrote:
>         > I should give an example
>         
>         
>         Thanks for that - I just two seconds ago sent a reply to the
>         list
>         complaining because you didn't provide one. However, the code
>         below
>         doesn't work.
>         
>         >
>         > for(p in 1:100)
>         > {
>         >     str <- paste("Task", p, sep=" ")
>         >     task_name[p] <- str
>         > }
>         
>         
>         We don't have task_name so this bit fails
>         
>         >
>         > ## first set the random seed so we get the same results
>         > set.seed(123)
>         > ## now produce some dummy data
>         > dummy <- data.frame(A = sample(LETTERS[1:4], 100, replace =
>         TRUE),
>         >                    B = sample(c("T","F"), 100, replace =
>         TRUE),
>         >                    C = rnorm(100))
>         >
>         > dummy <- cbind(dummy, task_name)
>         
>         
>         As does this bit
>         
>         >
>         > names(dummy)
>         >
>         > plot(dummy$A, dummy$task_name)
>         
>         
>         So we don't get the plot I think you wanted us to get here.
>         
>         If my reply (winging its way to you as we speak) doesn't solve
>         the
>         problem, then work on your example so that it works in a clean
>         R
>         session. If you start a new R session and paste in your
>         code/example,
>         you'll see if it runs OK or not.
>         
>         HTH
>         
>         G
>         
>         >
>         > if you look at the plot you see the task names are printed
>         but cut off.
>         >
>         > if I change the plot command using as.numeric I get task
>         names with numbers.
>         > It is still hard to interpret which number the task is
>         referring too?
>         >
>         
>         
>         >       [[alternative HTML version deleted]]
>         >
>         > ______________________________________________
>         > 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.
>         --
>         %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~
>         %~%~%~%
>          Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>          ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>          Pearson Building,             [e]
>         gavin.simpsonATNOSPAMucl.ac.uk
>          Gower Street, London          [w]
>         http://www.ucl.ac.uk/~ucfagls/
>          UK. WC1E 6BT.                 [w]
>         http://www.freshwaters.org.uk
>         %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~
>         %~%~%~%
>         
>         
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%




More information about the R-help mailing list