1

I am creating a 3x3 faceted graph using the code shown below. The problem is I get no legend.

# Create column vectors
XID <- rep(c(1,5,10), each=57)
TAD.unit <- c(0, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 6, 8, 10, 12, 16, 20, 24, 36, 48, 72)
TAD <- rep(TAD.unit, length=length(XID))
FID <-rep(c(1,2,3),each=length(TAD.unit),length=length(XID))
time <- TAD + (FID-1)*14*24
dist1 <- pweibull(TAD,2,2)
dist2 <- pweibull(TAD,2,4)

# Create data frame
data.df <- as.data.frame(cbind(XID,time, FID, dist1, dist2, TAD))

library(ggplot2)
label_both = function(column,value){paste(column,"=",value)}

# Create plot
my.plot1 <- ggplot(data.df, aes(x=TAD, y=dist1)) + geom_point() + 
  geom_line(aes(x=TAD, y=dist2)) +
  facet_grid(XID ~ FID, labeller=label_both) +
  labs(x = "TAD", y = "Response")

# alternative data structure per recommendation in
# http://stackoverflow.com/questions/15418302/ggplot2-how-to-show-the-legend?rq=1
library(reshape)
df.2 <- melt(data.df, id=c("XID","FID","TAD","time"))

I tried using data frame df.2 to see if that helps per the recommendation in the stack overflow thread I provide the link for. I tried various ggplot commands, but I still can't get it to work. Can someone please help me?

Also, how can I position the legend somewhere inside the 3x3 where there is blank space?

Thank you very much!

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
please help
  • 197
  • 1
  • 12

1 Answers1

0

As for the legend drawing, use your melted dataframe and e.g. use colour to distinguish your two distance datasets. E.g. like this:

ggplot(df.2, aes(x=TAD, y=value, colour = variable)) + geom_line() + facet_grid(XID ~ FID, labeller=label_both) + labs(x = "TAD", y = "Response")

And as for the legend positioning, I would refer you to this question with a good answer:

Position legend in first plot of facet

Community
  • 1
  • 1
dlaehnemann
  • 671
  • 5
  • 17
  • Thank you for that tip! I want to prepare the plot for a manuscript, so I prefer to keep things black and white and to have only a symbol for one response and a line for the other in order to visually discriminate better between the two responses. Any suggestions in that regard? – please help May 16 '13 at 14:36
  • Instead of `colour = variable` use `shape = variable`. More info on how to distinguish different data sets can be found here: http://docs.ggplot2.org/current/aes_linetype_size_shape.html – dlaehnemann May 16 '13 at 15:04
  • Thanks again 'thunk'. Shape = Variable keeps things b/w, but creates two lines, albeit no legend. I can of course then change the linetype to distinguish. The thing is, I provided sample data for SO. The real data is such that the two lines would virtually overlap. So that's why I want geom_point() for one variable and a geom_line() for the other. Within that context I wish to create a legend. – please help May 16 '13 at 15:15
  • Now, this is a different setup once again. In this case I would go back to your orginal dataframe (data.df) and create a different geom for the different distances as follows `ggplot(data.df, aes(x=TAD)) + geom_line(aes(y=dist1)) + geom_point(aes(y=dist2)) + facet_grid(XID ~ FID, labeller=label_both) + labs(x = "TAD", y = "Response")` -- hope that does what you're looking for! – dlaehnemann May 19 '13 at 12:34
  • Just say your comment today. I'll give it a try. Thank you for following up. – please help May 22 '13 at 16:00