美文网首页
R里建函数,把几个图画到一张上

R里建函数,把几个图画到一张上

作者: Holase | 来源:发表于2016-12-19 11:00 被阅读0次

    把几个图画在一起
    <pre>`
    library(ggplot2)
    library(grid)

    mydata <- read.csv("3cInCore.csv")

    p1 <- ggplot(mydata,aes(species,length))
    p1 <- p1 + geom_boxplot(aes(color = species),outlier.colour = "red",outlier.shape = 1)
    p1 <- p1 + theme(legend.position="none") ##remove legend
    p1 <- p1 + theme(text = element_text(size=20),axis.text.x = element_text(hjust=1))

    p2 <- ggplot(mydata,aes(species,width))
    p2 <- p2 + geom_boxplot(aes(color = species),outlier.colour = "red",outlier.shape = 1)
    p2 <- p2 + theme(legend.position="none")
    p2 <- p2 + theme(text = element_text(size=20),axis.text.x = element_text(hjust=1))

    p3 <- ggplot(mydata,aes(species,l.w.ratio))
    p3 <- p3 + geom_boxplot(aes(color = species),outlier.colour = "red",outlier.shape = 1)
    p3 <- p3 + theme(legend.position="none")
    p3 <- p3 + theme(text = element_text(size=20),axis.text.x = element_text(hjust=1))

    p4 <- ggplot(mydata,aes(species,stria.10))
    p4 <- p4 + geom_boxplot(aes(color = species),outlier.colour = "red",outlier.shape = 1)
    p4 <- p4 + theme(legend.position="none")
    p4 <- p4 + theme(text = element_text(size=20),axis.text.x = element_text(hjust=1))

    p5 <- ggplot(mydata,aes(species,K))
    p5 <- p5 + geom_boxplot(aes(color = species),outlier.colour = "red",outlier.shape = 1)
    p5 <- p5 + theme(legend.position="none")
    p5 <- p5 + theme(text = element_text(size=20),axis.text.x = element_text(hjust=1))

    combine 5 diagrams in one

    grid.newpage()
    pushViewport(viewport(layout = grid.layout(1, 5))) ## 1 row, 4 cols
    vplayout <- function(x, y){viewport(layout.pos.row = x, layout.pos.col = y)} ## choose the place of the diagram
    print(p1, vp = vplayout(1, 1))
    print(p2, vp = vplayout(1, 2))
    print(p3, vp = vplayout(1, 3))
    print(p4, vp = vplayout(1, 4))
    print(p5, vp = vplayout(1, 5))`
    </pre>

    相关文章

      网友评论

          本文标题:R里建函数,把几个图画到一张上

          本文链接:https://www.haomeiwen.com/subject/abftvttx.html