Centering of axis titles

76 views
Skip to first unread message

learnr

unread,
Sep 16, 2009, 9:40:52 AM9/16/09
to ggplot2
Consider the following code:

library(ggplot2)
df <- data.frame(a = c("longnameonthelefthandside"), b = c(sample
(10)))
ggplot(df, aes(b,a)) + geom_point() + xlab("label")

Is it possible to (automatically, rather than manually) center the x-
axis title in relation to the x-axis and not the overall plot area?

Thanks.

--
http://learnr.wordpress.com

baptiste auguie

unread,
Sep 17, 2009, 5:01:31 AM9/17/09
to learnr, ggplot2
As a workaround you could try calculating the width of your label,

library(ggplot2)
df <- data.frame(a = c("longnameonthelefthandside"), b = c(sample(10)))
p=ggplot(df, aes(b,a)) + geom_point() + xlab("label")

p + opts(axis.title.x = theme_text(size = 12,
hjust = 0.5 + convertUnit( # add offset due to ylabel, in npc units

grobWidth(textGrob(lab="longnameonthelefthandside", gp=gpar(cex=0.8)))
- # cex should be retrieved from theme options
unit(2,"line"),"npc",val=TRUE))) #
2 lines is probably only approximate


HTH,

baptiste
--
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

http://newton.ex.ac.uk/research/emag
______________________________

hadley wickham

unread,
Sep 18, 2009, 11:45:03 AM9/18/09
to learnr, ggplot2
> Is it possible to (automatically, rather than manually) center the x-
> axis title in relation to the x-axis and not the overall plot area?

This is a bug, and will be fixed along with the other layout bugs.

Hadley

--
http://had.co.nz/

Sebastian Weber

unread,
Aug 2, 2010, 6:27:16 AM8/2/10
to hadley wickham, ggplot2
Hi!

I am hit by this problem of axis labels not being centrered. Is the git version of ggplot already fixed or is it still on the todo list?

Sebastian

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the ggplot2 mailing list.
To post to this group, send email to ggp...@googlegroups.com
To unsubscribe from this group, send email to
ggplot2+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ggplot2
-~----------~----~----~----~------~----~------~--~---


Reply all
Reply to author
Forward
0 new messages