Setting the text of the axis and legend labels

14 views
Skip to first unread message

MTC

unread,
Jan 7, 2009, 11:53:54 AM1/7/09
to ggplot2
Greetings ggplot2 users:

Please forgive the following newbie question. I am sure the answer
must be covered in the ggplot book, but I cannot find it.

Can anyone tell me how to change the text for the axis labels and
(especially)
the legend? Chapter 2 (qplot) of the ggplot book shows that the
parameters
to qplot include xlab, ylab, and main, so these can be set if using
qplot, but I cannot find an explanation for how to do this from
ggplot. Also, this does not show how to set the text for the title of
the legend. Chapter 9 ("Polishing your plots for publication") shows
how to set the attributes of these strings using themes, but I do
not see how to change the text itself.

Thanks in advance for your help.

Matthew Clegg


hadley wickham

unread,
Jan 7, 2009, 8:10:08 PM1/7/09
to MTC, ggplot2
Hi Matthew,

> Can anyone tell me how to change the text for the axis labels and
> (especially)
> the legend? Chapter 2 (qplot) of the ggplot book shows that the
> parameters
> to qplot include xlab, ylab, and main, so these can be set if using
> qplot, but I cannot find an explanation for how to do this from
> ggplot. Also, this does not show how to set the text for the title of
> the legend. Chapter 9 ("Polishing your plots for publication") shows
> how to set the attributes of these strings using themes, but I do
> not see how to change the text itself.

The place to look is the scales chapter - the axis/legend title is a
property of the scale and so outside of qplot you need to modify the
corresponding scale.

Hadley

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

Matthew Clegg

unread,
Jan 8, 2009, 8:51:12 AM1/8/09
to hadley wickham, ggplot2
>
> The place to look is the scales chapter - the axis/legend title is a
> property of the scale and so outside of qplot you need to modify the
> corresponding scale.
>
> Hadley
>

Hi Hadley,

Thank you very much for your help with this. That was
exactly what I needed to know. Also, I would like to thank
David Kahle for helping me with this too.

In case anybody in the future has the same question, here
are the exact commands that I used:

# Create the ggplot object. df is a dataframe
# containing a column named 'dates' that will be
# used for the x-axis.
plot <- ggplot (df, aes(x=dates));

# Set the text for the x-axis. Note that since the data is
# of type Date, one must use scale_x_date. For other data,
# one might use scale_x_discrete or scale_x_continuous.
plot <- plot + scale_x_date("Date");

# Set the text for the y-axis.
plot <- plot + scale_y_continuous("Value");

# Set the text for the legend.
plot <- plot + scale_colour_discrete("Predictive model");

--
Matthew Clegg
matthew...@gmail.com

hadley wickham

unread,
Jan 8, 2009, 12:05:29 PM1/8/09
to Matthew Clegg, ggplot2

I should mention that there's a new (but not very well documented)
feature in the latest version. You can do:

plot + labs(x = "Date", y = "Value", colour = "Predictive model")

It saves a bit of typing!

Hadley

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

Mike Lawrence

unread,
Jan 8, 2009, 3:31:38 PM1/8/09
to ggplot2
Fantastic! I was going to submit a feature request for that very command :O)
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University
www.thatmike.com

Looking to arrange a meeting? Check my public calendar:
http://www.thatmike.com/mikes-public-calendar

~ Certainty is folly... I think. ~

hadley wickham

unread,
Jan 8, 2009, 3:37:44 PM1/8/09
to Mike Lawrence, ggplot2
And two even shorter short cuts: xlab("My x label") and ylab("My y
label") for even less typing in the most common cases.

Hadley
--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages