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

Stephen Sefick ssefick at gmail.com
Wed Mar 6 14:14:49 CET 2013


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:
>
> 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
>
>>
>> <((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .•
>
> `•. .><((((º>`•. . • `•. .• `•. .><((((º>
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> R-help at 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