Adding seperate features to facet plots

99 views
Skip to first unread message

Brian Weaver

unread,
Aug 3, 2010, 9:46:18 AM8/3/10
to ggp...@googlegroups.com
Hello All,

I am trying to add horizontal lines to plots that have been faceted (if that is  a word).  However, the different plots have their data centered around two different means, one being 1000 and the other being 4000.  My goal is to be able to add a horizontal line at 1000 to the plot where the data is centered around 1000 only and also a horizontal line at 4000 for the other plot only.  The following code may help get the idea across of what I want.  Both of the plots created by the facet have a horizontal line at 1000 when I only want the bottom one to and then likewise add a horizontal line at 4000 for the top plot.  Thank you in advance.

Brian

y.l <- rnorm(50,mean = 1000, sd = 0.001)
y.u <- rnorm(50,mean = 4000, sd = 0.001)

dat <- data.frame(Observe = round(c(y.l,y.u),digits = 3),Time = rep(1:50,2),Standard = c(rep("Low",50),rep("High",50)) )

qplot(Time,Observe,data = dat, geom = c("point","path")) + facet_grid(Standard~., scale = "free") + geom_smooth() +
geom_hline(aes(yintercept = 1000))

ONKELINX, Thierry

unread,
Aug 3, 2010, 10:27:16 AM8/3/10
to Brian Weaver, ggp...@googlegroups.com
Dear Brian,

Here are two solutions. Note that you can create the same dummy dataset
with much less code ;-)

Best regards,

Thierry

library(ggplot2)

#Solution 1: add the reference level to the entire dataset
dat <- expand.grid(Time = 1:50, Mean = c(1000, 4000))
dat$Observe <- round(rnorm(nrow(dat), mean = dat$Mean, sd = 0.01),
digits = 3)
dat$Standard <- factor(dat$Mean, labels = c("Low", "High"))

ggplot(dat, aes(x = Time, y = Observe)) + geom_hline(aes(yintercept =
Mean)) + geom_smooth() + geom_point() + geom_path() + facet_wrap(~
Standard, scales = "free", ncol = 1)

#solution 2: create an extra dataset with the reference level per
standard
Reference <- unique(dat[, c("Mean", "Standard")])
dat$Mean <- NULL
ggplot(dat, aes(x = Time, y = Observe)) + geom_hline(data = Reference,
aes(yintercept = Mean)) + geom_smooth() + geom_point() + geom_path() +
facet_wrap(~ Standard, scales = "free", ncol = 1)

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey


________________________________

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


Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.

Reply all
Reply to author
Forward
0 new messages