ggplot2 - R weird error "could not find function 'multiplot' " -
would plot 2 ggplots on 1 page. took illustration cookbook r , doesn't work. error could not find function "multiplot".
however ggplots plotable, reinstalled r, ggplot2, restarted, etc.. doing wrong?
library(ggplot2) # illustration uses chickweight dataset, comes ggplot2 # first plot p1 <- ggplot(chickweight, aes(x=time, y=weight, colour=diet, group=chick)) + geom_line() + ggtitle("growth curve individual chicks") # sec plot p2 <- ggplot(chickweight, aes(x=time, y=weight, colour=diet)) + geom_point(alpha=.3) + geom_smooth(alpha=.2, size=1) + ggtitle("fitted growth curve per diet") # 3rd plot p3 <- ggplot(subset(chickweight, time==21), aes(x=weight, colour=diet)) + geom_density() + ggtitle("final weight, diet") # 4th plot p4 <- ggplot(subset(chickweight, time==21), aes(x=weight, fill=diet)) + geom_histogram(colour="black", binwidth=50) + facet_grid(diet ~ .) + ggtitle("final weight, diet") + theme(legend.position="none") # no legend (redundant in graph) multiplot(p1, p2, p3, p4, cols=2)
quoting page link to:
the easy way utilize multiplot function, defined @ bottom of page. if isn't suitable needs, can re-create , modify it.
and code is:
# multiple plot function # # ggplot objects can passed in ..., or plotlist (as list of ggplot objects) # - cols: number of columns in layout # - layout: matrix specifying layout. if present, 'cols' ignored. # # if layout matrix(c(1,2,3,3), nrow=2, byrow=true), # plot 1 go in upper left, 2 go in upper right, , # 3 go way across bottom. # multiplot <- function(..., plotlist=null, file, cols=1, layout=null) { require(grid) # create list ... arguments , plotlist plots <- c(list(...), plotlist) numplots = length(plots) # if layout null, utilize 'cols' determine layout if (is.null(layout)) { # create panel # ncol: number of columns of plots # nrow: number of rows needed, calculated # of cols layout <- matrix(seq(1, cols * ceiling(numplots/cols)), ncol = cols, nrow = ceiling(numplots/cols)) } if (numplots==1) { print(plots[[1]]) } else { # set page grid.newpage() pushviewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # create each plot, in right location (i in 1:numplots) { # i,j matrix positions of regions contain subplot matchidx <- as.data.frame(which(layout == i, arr.ind = true)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } r ggplot2 syntax-error
No comments:
Post a Comment