Justification of axis title text

115 views
Skip to first unread message

Thomas

unread,
Dec 15, 2010, 9:29:18 AM12/15/10
to ggplot2
When making a standard plot using the ggplot2 package the
justification of axis titles seems to be relative to the plot area and
the area holding axis tick labels and title. This results in axis
titles that are sligthly off in relation to the plotting area when
using centre justification which I think is visually unnerving. Is
there any way one can change this behavior?

The link below shows an example. Here the x axis title should be
alligned with the 0 tickmark but is a bit off to the left because the
area to the left of the plotting area is included in the justification
calculations.

http://dl.dropbox.com/u/2323585/badjust.pdf

Best

Thomas Pedersen
Master student at University of Copenhagen

Dennis Murphy

unread,
Dec 15, 2010, 1:55:33 PM12/15/10
to Thomas, ggplot2
Hi:

Without answering your question directly, it might be useful to use coord_equal() in the plot call and pull the legend inside the plot (e.g., in the upper right corner). That may remove the titling problem by default. The legend can be moved inside the plot region by using opts(legend.position = c(x, y)), where x and y are relative horizontal and vertical positions with respect to the entire plot area, including the outside spacing. Here's a simple example to show how to pull a legend within the plot region- I used a bit of trial and error to get the position I wanted in the end:

df <- data.frame(gp = factor(rep(LETTERS[1:3], each = 50)),
                   y = rnorm(150, m = rep(c(1, 3, 5), each = 50), 1))
ggplot(df, aes(x = gp, y = y, fill = gp)) +  geom_boxplot() +
    opts(legend.position = c(0.11, 0.9))

In your case, it's clear that both variables have the same range and 'units', so maximum use of the plot area along with an equal scaled plot would seem to be the best strategy here. It also eliminates an aspect ratio distortion of the ellipse, especially if it was meant to be a circle :)

HTH,
Dennis


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Thomas

unread,
Dec 16, 2010, 5:49:51 AM12/16/10
to ggplot2
Thanks for the response. The problem is not the right side where the
legends are though. As you can see from the plot your code produce the
x axis title is still not perfectly aligned with the centre of the
plotting region.

And the elipse is not supposed to be circular, but thanks : )

Thomas
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com­>
> > More options:http://groups.google.com/group/ggplot2- Skjul tekst i anførselstegn -
>
> - Vis tekst i anførselstegn -

baptiste auguie

unread,
Dec 16, 2010, 8:46:29 AM12/16/10
to Thomas, ggplot2
Hi,

I don't think there is a good solution to this problem in the current
situation. Consider the following example,


library(ggplot2)

my.title <- function(...)
function(label, x = 0.5, y = 0.5, ..., vjust = 0.5,
hjust = 0.5, default.units = "npc") {
tg <- textGrob(label, x, y, hjust = hjust, vjust = vjust, ...)
rg <- rectGrob(x=1, width=unit(1, "npc"), just="right",
gp=gpar(col="red", fill="grey95"))
gTree(children=gList(rg, tg))
}

qplot(1, "aaaaaaaaaaaaaaaaa") + opts(axis.title.x = my.title())


What happens (in plot-render.r) is that the plot panel and the x axis
title viewports share the same width in a Grid layout. Unfortunately,
the plot panel has to accommodate for the y axis tick marks and
labels, and there is no way to know their extent in advance.
A better approach, imho, would have all panel annotations in a
separate viewport.

All the best,

baptiste

> More options: http://groups.google.com/group/ggplot2
>

Reply all
Reply to author
Forward
0 new messages