[R] ggplot2 - Problem with grid plot

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Mar 4 13:41:30 CET 2008


Dear Chris,

You'll need to add information on the facet factor (color) to the
medians dataset.

library(ggplot2)
medians <- aggregate(diamonds$price, list(cut = diamonds$cut, color =
diamonds$color), median)
ggplot(data=diamonds, aes(x=price)) + geom_density(aes(color=cut)) +
geom_vline(data=medians, aes(color=cut, intercept = x)) +
facet_grid(color~.)

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Chris Friedl
Verzonden: dinsdag 4 maart 2008 13:17
Aan: r-help op r-project.org
Onderwerp: [R] ggplot2 - Problem with grid plot


Hi R-help

I'm trying to create a grid plot in which each plot in the grid contains
two
density plots (colored by factor) and two vertical lines at the
respective
medians (also colored by the factor).

Using the diamonds dataset as an example, the following commands give me
price density plots by factor cut in a single, ungridded plot.

p <- ggplot(data=diamonds, aes(x=price)) + geom_density(aes(color=cut))
p


I can add vertical lines at the medians of each density as follows.

medians <- data.frame(cut=levels(diamonds$cut),
intercept=tapply(diamonds$price, list(diamonds$cut), median))
p + geom_vline(data=medians, aes(color=factor(cut)))



I can create a gridded plot of price density by factors cut and color
(where
color is the grid reference) as follows.

p + facet_grid(color~.)


However I've had no luck trying to now add vertical grid lines on each
density plot within each grid. Can anyone offer a suggestion on how to
do
this?

Is this a limitation with ggplot2? I'm using ggplot2 0.5.7, R 2.6.2,
Windows
XP.


-- 
View this message in context:
http://www.nabble.com/ggplot2---Problem-with-grid-plot-tp15825952p158259
52.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help op r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list