margin with element_text

1,295 views
Skip to first unread message

Mark Seeto

unread,
May 18, 2016, 1:46:36 AM5/18/16
to ggplot2
Dear ggplot2 group,

Is it the case that in ggplot2 2.1.0, margins can be used to move "axis.title.x" up or down, but not left or right?

Example:

d <- data.frame(x = rnorm(20), y = rnorm(20))

qplot(x, y, data=d) +
  theme(axis.title.x = element_text(margin = margin(t = 50)))  # works as expected

qplot(x, y, data=d) +
  theme(axis.title.x = element_text(margin = margin(b = 50)))  # works as expected

qplot(x, y, data=d) +
  theme(axis.title.x = element_text(margin = margin(r = 50)))  # no sideways movement of axis.title.x

The last plot is slightly different to the plot obtained from qplot(x, y, data=d), but the difference appears to be in the up/down direction, not in the left/right direction.

It's not likely that I'll want to move axis.title.x to the left or right, but I'd like to understand what's possible.

Thanks,

Mark



Crump, Ron

unread,
May 18, 2016, 4:10:43 AM5/18/16
to Mark Seeto, ggplot2
Hi Mark,

>Is it the case that in ggplot2 2.1.0, margins can be used to move
>"axis.title.x" up or down, but not left or right?

See ?element_text and ?margin

I'm going to make a mess of this; but my understanding of margins, which
is minimal, is that they define a clear space around the element's
container. This may result in a shift in the element/change in other bits
of the plot to facilitate that. If you look at ?element_text it says:

"When creating a theme, the margins should be placed on the side of the
text facing towards the center of the plot."

Which would imply that the left and right margins are not relevant for
axis.title.x. Do they act if set to a really big value? Or if something
else is placed at the same horizontal level (i.e. something to keep clear
of?)? I don't know, but I can see that the margin is not the way to move
the axis.title.x about.

To move the element within it's container (grid object?) you'd want to be
using hjust and vjust (hjust to shift axis.title.x left or right). If
using vjust, you might need to specify the lineheight in the theme call
too.

Hope this helps a bit.

Ron.

Mark Seeto

unread,
May 18, 2016, 5:08:20 AM5/18/16
to ggplot2
Thanks for your reply, Ron.

I had read ?element_text and ?margin, but they didn't answer my question, as far as I could see. While ?element_text says "When creating a theme, the margins should be placed on the side of the text facing towards the center of the plot", specifying a bottom margin for axis.title.x still works, so I thought left and right margins might work too. Also, it's not clear to me whether adjusting a theme element constitutes "creating a theme".

You're right that hjust can be used to move axis.title.x left and right (although vjust doesn't seem to do anything to axis.title.x), but at https://github.com/hadley/ggplot2/issues/1435 Hadley says "Using hjust has always been a hack. Please use the new margins parameter instead", so I thought I should try to use margins rather than hjust and vjust.

Mark

Hadley Wickham

unread,
May 18, 2016, 8:51:15 AM5/18/16
to Mark Seeto, ggplot2
Given that the x axis is centred horizontally, what did you expect
setting the left and right margins to do?

Hadley
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://hadley.nz

Mark Seeto

unread,
May 18, 2016, 5:11:43 PM5/18/16
to ggplot2, mark...@gmail.com
On Wednesday, May 18, 2016 at 10:51:15 PM UTC+10, Hadley Wickham wrote:
Given that the x axis is centred horizontally, what did you expect
setting the left and right margins to do?


Thanks for your reply Hadley, and thanks for ggplot2. If there's something in the documentation saying that axis.title.x is forced to remain centred horizontally then I missed it, so I expected setting the left or right margins to move the x axis title right or left.

From your statement "Using hjust has always been a hack. Please use the new margins parameter instead" (link in previous reply), I inferred (apparently incorrectly) that what could be done with hjust and vjust in version 1.0.1 can be done with margins in version 2.1.0.

I see that in version 2.1.0, hjust still moves axis.title.x left or right, so my conclusion is (please correct me if I'm wrong) that to move axis.title.x vertically, margins should be used, but in the unlikely event that I want to move axis.title.x horizontally, hjust should be used.

[The reason I'm looking at this sort of thing is that I only recently started using version 2.1.0, having previously used 1.0.1, and I'm trying to understand the changes.]

Mark


Reply all
Reply to author
Forward
0 new messages