hjust and vjust

653 views
Skip to first unread message

martin_kaercher

unread,
Jul 24, 2010, 4:47:17 PM7/24/10
to ggplot2
Dear all,

I'm a beginner in R and ggplot2 and also new to this group. Having
read the ggplot2 book and some parts of the ggplot2 homepage I
managed to get that far:

ggplot(df, aes(y = resp, x = trt, group = group, colour = group)) +
scale_colour_manual(values = c("#EE00EE", "#0000FF")) +
geom_smooth(aes(ymax = resp + se, ymin = resp - se), data = df, stat
= "identity", size = 1) +
opts(legend.position = c(0.3, 0.36), legend.background =
theme_rect(colour = 0)) +
opts(legend.key = theme_rect(colour = 0)) +
opts(legend.title = theme_blank()) +
opts(legend.text = theme_text(size = 17)) +
labs(x = "day", y = "proportion of eggs remaining") +
scale_x_discrete(expand = c(0, 0.1)) + scale_y_continuous(expand =
c(0, 0), breaks = c(0.90, 0.95, 1.00)) +
opts(axis.text.x = theme_text(size = 15, vjust = 1), axis.text.y =
theme_text(size = 15, hjust = 1)) +
opts(axis.title.x = theme_text(size = 25, hjust = 0.541),
axis.title.y = theme_text(size = 25, angle = 90, vjust = 0.51))

However, I couldn't find out how “vjust” and “hjust” work together
without influencing each other which is why I currently used only
“vjust” or “hjust”. Anytime I used both of them it just scrambled
everything up. I would be most grateful for an explanation how to use
them at the same time so that I can have the axis title centred and a
bit further apart from the y-axis. I tried to attched the PDF of it to
this message but couldn't find out how to do it in "google groups".
Thus, how can I attach PDFs, scripts or data sets?

Cheers,

Martin

p.s.: My general R knowledge is almost zero. I have only read Hadley's
ggplot2 book, parts of Zuur et al.'s “A beginner's guide to R”, and
parts of Zuur et al.'s “Mixed Effects Models and Extensions in Ecology
with R”. Thus, I really struggle with R and would be most grateful for
advice on essential R literature. Is the book R Graphics by Paul
Murrell useful for ggplot2 as well or only for plot? Is there
additional literature on ggplot2, ideally a list of all commands that
can be used?

fernando

unread,
Jul 25, 2010, 10:15:45 AM7/25/10
to martin_kaercher, ggplot2
Hi all,

I think the code bellow illustrates what Martin is trying to say. Note that
although not considered in the example, everything works ok if labels are
not rotated.
In my opinion, especially after seeing Baptiste example (e.g. textGrob does
its job ok, even if you include rot = 90), this may be a bug in ggplot2.

Hope this helps,
fernando


library(ggplot2)
p <- qplot(1, 1) + scale_y_continuous(name = "A axis title")
p1 <- p + opts(axis.title.y = theme_text(size = 25, angle = 90, vjust = 0.5,
hjust = 0.5),
title = "vjust = 0.5, hjust = 0.5")
p2 <- p + opts(axis.title.y = theme_text(size = 25, angle = 90, vjust = 0.5,
hjust = 1),
title = "vjust = 0.5, hjust = 1")
p3 <- p + opts(axis.title.y = theme_text(size = 25, angle = 90, vjust = 1,
hjust = 0.5),
title = "vjust = 1, hjust = 0.5")
p4 <- p + opts(axis.title.y = theme_text(size = 25, angle = 90, vjust = 0,
hjust = 0.5),
title = "vjust = 0, hjust = 0.5")

pushViewport(viewport(layout = grid.layout(2, 2)))
print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
print(p3, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(p4, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))


-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
martin_kaercher
Enviado el: sábado, 24 de julio de 2010 22:47
Para: ggplot2
Asunto: hjust and vjust

Dear all,

Cheers,

Martin

--
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

baptiste auguie

unread,
Jul 25, 2010, 10:56:30 AM7/25/10
to fernando, ggplot2
I think there's a problem with theme_text() for axis titles: it
adjusts both the position in the axis title viewport and the
adjustment, which results in the strange behavior pointed out here. It
is most obvious when the string is rotated since the order in which
the rotation and the translation are applied matter.

This can be illustrated by comparing theme_text with an altered
version which keeps x=0.5, y=0.5:

library(ggplot2)
p <- qplot(1, 1, geom="blank") +
ylab("y-axis\ntitle") + theme_bw()

hvjust <- expand.grid(hjust=seq(0, 1, length=3),
vjust=seq(0, 1, length=3))

foo <- function(hjust = 0.5, vjust = 0.5, angle = 0)
{
vj <- vjust
hj <- hjust
structure(function(label, x = 0.5, y = 0.5, ..., vjust = vj,
hjust = hj, default.units = "npc") {
textGrob(label, x, y, hjust = hjust, vjust = vjust, ...,
default.units = default.units, rot = angle)
}, class = "theme", type = "text", call = match.call())
}


test <- function(hjust, vjust, fun=theme_text){
p + opts(axis.title.y = fun(angle = 90,
vjust = vjust, hjust = hjust),
title = paste("hjust:", hjust,
", vjust:", vjust, sep=""))
}

gl <- mlply(hvjust, test)
gl2 <- mlply(hvjust, test, fun=foo)

source("http://gridextra.googlecode.com/svn/trunk/R/arrange.r")
do.call(grid.arrange, gl)

# with foo, it's fine
do.call(grid.arrange, gl2)


Best,

baptiste

Martin Kärcher

unread,
Jul 25, 2010, 6:41:32 PM7/25/10
to Brandon Hurr, ggp...@googlegroups.com
Dear Brandon,

Thanks a lot for help! I am amazed how quickly you, Babtiste and Fernando replied to my question, especially on a Sunday. Great! Next time I will upload all necessary material to GoogleDocs and provide a link. However, is it okay to just send a question to the ggplot2 group (ggp...@googlegroups.com) directly from "Yahoo" in the first place or would this have to be via "Google Groups"?

Anyway, I typed in "dput(df)" as you suggested and received:

structure(list(trt = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("1",
"2", "3")), resp = c(1, 0.940193416337148, 0.904454585871723,
1, 0.97865467535666, 0.958563947267053), group = structure(c(2L,
2L, 2L, 1L, 1L, 1L), .Label = c("female", "male"), class = "factor"),
    se = c(0, 0.0168994661360938, 0.0295728999211006, 0, 0.00549126551165786,
    0.00824844616926236)), .Names = c("trt", "resp", "group",
"se"), row.names = c(NA, -6L), class = "data.frame")

Is this what you meant?


If I type in "df" I receive:

  trt      resp  group          se
1   1 1.0000000   male 0.000000000
2   2 0.9401934   male 0.016899466
3   3 0.9044546   male 0.029572900
4   1 1.0000000 female 0.000000000
5   2 0.9786547 female 0.005491266
6   3 0.9585639 female 0.008248446


This time I also attached the PDF of the graph.

Many thanks and best wishes,

Martin



Martin H. Kärcher

Laboratory of Apiculture & Social Insects

Department of Biology & Environmental Science

School of Life Sciences

John Maynard Smith Building

University of Sussex

Brighton BN1 9QG, UK


landline (Austria): +43 316 382448

mobile (Austria): +43 650 3824481

landline (England): +44 127 387 7198

http://www.sussex.ac.uk/biology/profile220918.html




Von: Brandon Hurr <bhi...@gmail.com>
An: martin_kaercher <martin_...@yahoo.de>
CC: ggplot2 <ggp...@googlegroups.com>
Gesendet: Sonntag, den 25. Juli 2010, 14:34:45 Uhr
Betreff: Re: hjust and vjust

Martin, 

In the google groups interface it doesn't look like it's possible. If you receive the ggplot2 messages in gmail you should be able to attach something like you would in any other email. You can also upload your PDF to googleDocs or some other file upload service and provide a link. 

Do you think you could provide the data behind the graph so we could see what you're going for? 

Run 

dput(df)

and paste the output here. 

Thanks, 

Brandon




A.plot5.pdf
Reply all
Reply to author
Forward
0 new messages