customize y-axis label of a density plot

20 views
Skip to first unread message

Philippe Glaziou

unread,
Jan 13, 2009, 12:53:28 PM1/13/09
to ggp...@googlegroups.com
Dear List,

I would like to change the label for the y-axis of a density plot:

> qplot (rnorm(100), geom='density') + ylab ('Density')

however, the ylab() function doesn't seem to work here (nor does the
labs() variant) and the y-axis label remains the default 'density'.
Passing a ylab argument to qplot() doesn't seem to work either. I
copied details of my session below my signature. This is for a
publication and I would need the label to read 'Density' with capital
D.

Heartfelt thanks to Hadley for ggplot2, a truly wonderful package.

Any workaround / help highly appreciated,

Philippe

> sessionInfo()
R version 2.8.1 (2008-12-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats graphics grDevices datasets grid tcltk utils
methods base

other attached packages:
[1] car_1.2-10 arm_1.1-17 R2WinBUGS_2.1-8 coda_0.13-3
[5] lme4_0.999375-28 Matrix_0.999375-17 MASS_7.2-45 lattice_0.17-17
[9] foreign_0.8-30 pg_1.0 gmodels_2.14.1 ggplot2_0.8.1
[13] reshape_0.8.2 plyr_0.1.4 proto_0.3-8 svSocket_0.9-5
[17] TinnR_1.0.2 R2HTML_1.59 Hmisc_3.4-4

loaded via a namespace (and not attached):
[1] cluster_1.11.11 gdata_2.4.2 gtools_2.5.0
RColorBrewer_1.0-2 svMisc_0.9-5
[6] tools_2.8.1

hadley wickham

unread,
Jan 13, 2009, 12:58:37 PM1/13/09
to Philippe Glaziou, ggp...@googlegroups.com
Hi Philippe,

For this special case, you need to use the full form: +
scale_y_continuous("Density")

The reason is because the y axis is a computed variable, so at the
time you create the plot, ggplot doesn't know whether it will be a
continuous or discrete variable and so doesn't add a default scale
(like it does for all the other variables). xlab(), and labels() and
the xlab argument all working by modifying the existing scale, so they
don't work in this case because it doesn't exist.

Regards,

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

Philippe Glaziou

unread,
Jan 13, 2009, 1:03:06 PM1/13/09
to hadley wickham, ggp...@googlegroups.com
2009/1/13 hadley wickham <h.wi...@gmail.com>:

> For this special case, you need to use the full form: +
> scale_y_continuous("Density")
>
> The reason is because the y axis is a computed variable, so at the
> time you create the plot, ggplot doesn't know whether it will be a
> continuous or discrete variable and so doesn't add a default scale
> (like it does for all the other variables). xlab(), and labels() and
> the xlab argument all working by modifying the existing scale, so they
> don't work in this case because it doesn't exist.


brilliant!

thanks so much for such incredible support

Philippe

Reply all
Reply to author
Forward
0 new messages