hjust and vjust, not working for the legend text anymore?

518 views
Skip to first unread message

nzcoops

unread,
Jul 17, 2012, 9:59:38 PM7/17/12
to ggplo...@googlegroups.com
This might be old news, but it seems hjust and vjust are no longer legend text.

If this is old news, what is the best way to stay in the loop with these things coming and going?

This example is straight out of the github examples:
https://github.com/hadley/ggplot2/wiki/Legend-Attributes

xy <- data.frame(x=1:10, y=10:1, type = rep(LETTERS[1:2], each=5)) plot <- ggplot(data = xy)+ geom_point(aes(x = x, y = y, color=type))
plot + opts(legend.text = theme_text(colour = 'red', angle = 45, size = 10, hjust = 3, vjust = 3, face = 'bold'))

nzcoops

unread,
Jul 18, 2012, 12:38:05 AM7/18/12
to ggplo...@googlegroups.com
Okay, to expand on this, having looked in guides via joran on SO.

p <- ggplot(mtcars, aes(x=wt, y=mpg, colour=factor(cyl))) + geom_line()
p + opts(legend.title=theme_text(size = 15))
# great that bumped the font size up, the 'original/old way'

# now use the new guides options
p + guides(colour = guide_legend(title.theme = theme_text(size = 8)))
p + guides(colour = guide_legend(title.theme = theme_text(size = 8), title.position="left"))
p + guides(colour = guide_legend(title.theme = theme_text(size = 8), title.position="left", title.vjust=2))
p + guides(colour = guide_legend(title.theme = theme_text(size = 8), title.position="left", title.vjust=2, title.hjust=1))
# so we can change the font size, move it to one size, change vertical alignment but not horiztonal?

# but what if it's on top?
p + guides(colour = guide_legend(title.theme = theme_text(size = 8)))
p + guides(colour = guide_legend(title.theme = theme_text(size = 8), title.hjust=1))
p + guides(colour = guide_legend(title.theme = theme_text(size = 8), title.hjust=1, title.vjust=2))
# can change size but not alignment

# what about nrow
p + guides(colour = guide_legend(nrow=2,title.theme = theme_text(size = 8)))
p + guides(colour = guide_legend(nrow=2,title.theme = theme_text(size = 8), title.hjust=1))
p + guides(colour = guide_legend(nrow=2,title.theme = theme_text(size = 8), title.hjust=1, title.vjust=2))
# so when it's on top we can adjust the horizontal but not the vertical alignment


From this I conclude that we can't tweak vjust when the title is on top, can't tweak hjust when it's the on the side and can only tweak hjust on the top when it's forced wider with nrow. To me this isn't optimal functionality and is more restrictive than what we used to have? Or is there something I'm missing.

The way I came across this was through tweaking a graph for publication, in a tight space (2.6" wide output), and needing to move the legend title just a fraction. I wrote the theme for this particular graph I'm doing a few months ago, then noticed it wasn't doing now what it did then, then noticed the move to 'guides' via joran @SO, then wound up here(/ in this position).

Cheers
Reply all
Reply to author
Forward
0 new messages