r - Adding a legend to ggplot with facet_grid -


i creating 3x3 faceted graph using code shown below. problem 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 see if helps per recommendation in stack overflow thread provide link for. tried various ggplot commands, still can't work. can please me?

also, how can position legend somewhere inside 3x3 there blank space?

thank much!

as legend drawing, use melted dataframe , e.g. use colour distinguish 2 distance datasets. e.g. 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 legend positioning, refer question answer:

position legend in first plot of facet


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -