Dear Thierry,

thank you! this was exactly what I wanted. Just one question: If I use the 
facet_wrap(~Grouping, ncol=2) and not ncol=3, how can I add an x-axis to the 
plot that is missing one? I think that the ncol=2 looks nicer (like you have 
suggested) than the ncol=3, which would avoid this problem.
I also found:
theme(legend.justification=c(1,0), legend.position=c(1,0))
for the legend justification. Now this starts to look like how I want it to! 
Thank you so much for your time!

Best regards


Anna Zakrisson Braeunlich
PhD student

Department of Ecology Environment and Plant Sciences
Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden

Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin - Kreuzberg
Germany/Deutschland

E-mail: anna.zakrisson@su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• 
`•. .><((((º>`•. . • `•. .• `•. .><((((º>

-----Original Message-----
From: "ONKELINX, Thierry" <Thierry.ONKELINX@inbo.be>
To: Anna Zakrisson <anna@ecology.su.se>, "r-help@r-project.org" 
<r-help@r-project.org>
Date: Wed, 6 Mar 2013 13:36:00 +0000
Subject: RE: [R] Ggplot2: Moving legend,   change fill and removal of space 
between   plots when using   grid.arrange() possible use of facet_grid?

Dear Anna,

Is this what you would like?

Summ  <-   ddply(mydata, .(factor3,factor1), summarize,
                       mean = mean(var1, na.rm = FALSE),
                       sdv = sd(var1, na.rm = FALSE),
                       se = 1.96*(sd(var1, na.rm=FALSE)/sqrt(length(var1))))
Summ$Grouping <- c("AB", "AB", "CD", "CD", "EF", "EF")[Summ$factor1]
Summ$factor1bis <- c("0", "1", "0", "1", "0", "1")[Summ$factor1]

ggplot(Summ, aes(factor3, mean, group = factor1bis, shape = factor1bis, 
linetype = factor1bis, ymin = mean - sdv , ymax = mean + sdv)) +
geom_point(position = position_dodge(width = 0.25), size = 3) +
geom_line(position = position_dodge(width = 0.25)) +
geom_errorbar(width = 0.3, position = position_dodge(width = 0.25), size = 
0.3) +
facet_wrap(~Grouping, ncol = 2) +
theme_bw() +
ylab(expression(paste("my measured stuff"))) +
xlab("factor3") +
labs(shape = "factor1", group = "factor1", linetype = "factor1")

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and 
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx@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

-----Oorspronkelijk bericht-----
Van: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org] 
Namens Anna Zakrisson
Verzonden: woensdag 6 maart 2013 13:33
Aan: r-help@r-project.org
Onderwerp: [R] Ggplot2: Moving legend, change fill and removal of space 
between plots when using grid.arrange() possible use of facet_grid?

Hi,

# For publications, I am not allowed to repeat the axes. I have tried to
remove the axes using:
# yaxt="n", but it did not work. I have not understood how to do this in
ggplot2. Can you help me?
# I also do not want loads of space between the graphs (see below script
with Dummy Data).
# If I could make it look like the examples on the (nice) examples page:
# http://www.ling.upenn.edu/~joseff/rstudy/summer2010_ggplot2_intro.html
# using the facet_grid(), I would be very very happy.

# I also do not want the gemoetric points to be filled and the fill="white"
commande
# does not seem to work - why? and are there alternatives?

#Furthermore, I would like to add legends to inside the plot area instead of
on the side. Like when you use plotrix() and brkdn.plot:
legend("topright", c("A", "B"), pch=c(0,1), bg="white",
       lty = 1:2, cex=1, bty="n")
# This did not work in ggplot2. What are my alternatives. I have extensively
searched the internet and have I missed something obvious, it was due to
   # tiredness and not to lazyness.

# Some dummy data:
mydata<- data.frame(factor1 = factor(rep(LETTERS[1:6], each = 80)),
                  factor2 = factor(rep(c(1:5), each = 16)),
                  factor3 = factor(rep(c(1:4), each = 4)),
                  var1 = rnorm(120, mean = rep(c(0, 3, 5), each = 40),
                  sd = rep(c(1, 2, 3), each = 20)),
                  var2 = rnorm(120, mean = rep(c(6, 7, 8), each = 40),
                  sd = rep(c(1, 2, 3), each = 20)))


# Splitting data into 3 data frames (based on factor1)
# If I could do this using for example facet_wrap() or facet_grid(), I would
be very
# happy! I have tried but failed that method.

DataAB <- mydata[(mydata$factor1) %in% c("A", "B"), ]
DataCD <- mydata[(mydata$factor1) %in% c("C", "D"), ]
DataEF <- mydata[(mydata$factor1) %in% c("E", "F"), ]
DataAB
library(plyr)
library(ggplot2)

#Plot: levels A and B:
# Summary (means etc)
SummAB  <-   ddply(DataAB, .(factor3,factor1), summarize,
                       mean = mean(var1, na.rm = FALSE),
                       sdv = sd(var1, na.rm = FALSE),
                       se = 1.96*(sd(var1, na.rm=FALSE)/sqrt(length(var1))))
SummAB
p1  <-  ggplot(SummAB, aes(factor3, mean,
                               colour = factor1, group = factor1,
                               shape = factor1)) +
  geom_point(aes(shape=factor(factor1)), color="black", fill="white",
             position = "dodge", width = 0.3, size=3) +
  geom_line(aes(linetype=factor1), color = "black", size = 0.5) +
  geom_errorbar(aes(ymin = mean - sdv , ymax = mean + sdv), width = 0.3,
                position = "dodge", color = "black", size=0.3) +
  theme_bw() +
  ylab(expression(paste("my measured stuff"))) +
  xlab("factor3") + ggtitle("") +
  labs(color = "factor1", shape = "factor1", group = "factor1",
       linetype = "factor1")
p1

#Plot: levels C and D:
# Summary (means etc)
SummCD  <-   ddply(DataCD, .(factor3,factor1), summarize,
                   mean = mean(var1, na.rm = FALSE),
                   sdv = sd(var1, na.rm = FALSE),
                   se = 1.96*(sd(var1, na.rm=FALSE)/sqrt(length(var1))))

p2  <-  ggplot(SummCD, aes(factor3, mean,
                           colour = factor1, group = factor1,
                           shape = factor1)) +
  geom_point(aes(shape=factor(factor1)), color="black", fill="white",
             position = "dodge", width = 0.3, size=3) +
  geom_line(aes(linetype=factor1), color = "black", size = 0.5) +
  geom_errorbar(aes(ymin = mean - sdv , ymax = mean + sdv), width = 0.3,
                position = "dodge", color = "black", size=0.3) +
  theme_bw() +
  ylab(expression(paste("my measured stuff"))) +
  xlab("factor3") + ggtitle("") +
  labs(color = "factor1", shape = "factor1", group = "factor1",
       linetype = "factor1")
p2

#Plot: levels C and D:
# Summary (means etc)
SummEF  <-   ddply(DataEF, .(factor3,factor1), summarize,
                   mean = mean(var1, na.rm = FALSE),
                   sdv = sd(var1, na.rm = FALSE),
                   se = 1.96*(sd(var1, na.rm=FALSE)/sqrt(length(var1))))

p3  <-  ggplot(SummEF, aes(factor3, mean,
                           colour = factor1, group = factor1,
                           shape = factor1)) +
  geom_point(aes(shape=factor(factor1)), color="black", fill="white", #Why
is the fill commando not working?
             position = "dodge", width = 0.3, size=3) +
  geom_line(aes(linetype=factor1), color = "black", size = 0.5) +
  geom_errorbar(aes(ymin = mean - sdv , ymax = mean + sdv), width = 0.3,
                position = "dodge", color = "black", size=0.3) +
  theme_bw() +
  ylab(expression(paste("my measured stuff"))) +
  xlab("factor3") + ggtitle("") +
  labs(color = "factor1", shape = "factor1", group = "factor1",
       linetype = "factor1")
p3

ary(gridExtra)
sidebysideplot <- grid.arrange(p1, p2, p3, ncol=2)


Anna Zakrisson Braeunlich
PhD student

Department of Ecology Environment and Plant Sciences
Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden

Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin - Kreuzberg
Germany/Deutschland

E-mail: anna.zakrisson@su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

><((((Âº>`â€¢. . â€¢ `â€¢. .â€¢ `â€¢. . 
><((((Âº>`â€¢. . â€¢ `â€¢. .â€¢
`â€¢. .><((((Âº>`â€¢. . â€¢ `â€¢. .â€¢ `â€¢. 
.><((((Âº>

        [[alternative HTML version deleted]]

* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
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.

	[[alternative HTML version deleted]]

