Skip to content

Ggplot2: Moving legend, change fill and removal of space between plots when using grid.arrange() possible use of facet_grid?

13 messages · stephen sefick, ONKELINX, Thierry, Anna Zakrisson +1 more

#
Look at the function melt and use the factor columns as id variables.  
You should be able to do what you want with facet_grid. I have found 
inkscape useful to build legends and modify axes labels.  I know this is 
only a partial answer, but I hope this helps.

Stephen
On Wed 06 Mar 2013 06:32:42 AM CST, Anna Zakrisson wrote:
#
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 at 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 at r-project.org [mailto:r-help-bounces at r-project.org] Namens Anna Zakrisson
Verzonden: woensdag 6 maart 2013 13:33
Aan: r-help at 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 at su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b
`???. .><((((??>`???. . ??? `???. .??? `???. .><((((??>


* * * * * * * * * * * * * 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.
#
For the simplist of the issues use  scale_shape(solid = FALSE) to get hollow points
 Using your data (below) this seems to work
p1  <-  ggplot(SummAB, aes(factor3, mean,
          colour = factor1, group = factor1,shape = factor1)) + 
        scale_y_continuous(guide_legend(legend.position=c(4 ,6))) +
       scale_shape(solid = FALSE) + guides(colour = guide_legend(title.position = "right")) +
       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

John Kane
Kingston ON Canada
____________________________________________________________
Receive Notifications of Incoming Messages
Easily monitor multiple email accounts & access them with a click.
Visit http://www.inbox.com/notifier and check it out!
#
Placing a legend.  

 z <- ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) + geom_point()
  z + theme(legend.position = c(.5, .5))
  
Currently this does not appear to work in RStudio but seems fine if I use gedit or if I run R in a terminal session.  

John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
#
Replying to my own post RStudio is doing this fine once I had rebooted R.  I must have had some strange stuff loaded that I had not realised was there.

John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
#
Looking good.  I think the function in this post is what you want. It worked on your code for me.  http://stackoverflow.com/questions/13297155/add-floating-axis-labels-in-facet-wrap-plot .  

John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!