日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ggplot2画图

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ggplot2画图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ggplot2 畫圖的輔助函數

# dual y-axis plot function # dual_axis_plot=function(p1,p2){require(grid)require(gtable)grid.newpage()## convert plots to gtable objectsg1 <- ggplot_gtable(ggplot_build(p1)) g2 <- ggplot_gtable(ggplot_build(p2)) # overlap the panel of 2nd plot on that of 1st plot pp <- c(subset(g1$layout, name == "panel", se = t:r)) g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l) # axis tweaks ia <- which(g2$layout$name == "axis-l")ga <- g2$grobs[[ia]]ax <- ga$children[[2]]ax$widths <- rev(ax$widths)ax$grobs <- rev(ax$grobs)ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) +0.1)g <- gtable_add_grob(g, ax, pp$t, length(g$widths) +0.1, pp$b)ia <- which(g2$layout$name == "ylab")ga <- g2$grobs[[ia]]ga$rot <- 270g = gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) +0.1)g = gtable_add_grob(g, ga, pp$t, length(g$widths) +0.1 , pp$b)grid.draw(g) }# Multiple plot function # # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) # - cols: Number of columns in layout # - layout: A matrix specifying the layout. If present, 'cols' is ignored. # # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), # then plot 1 will go in the upper left, 2 will go in the upper right, and # 3 will go all the way across the bottom. # multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {library(grid)# Make a list from the ... arguments and plotlistplots <- c(list(...), plotlist)numPlots = length(plots)# If layout is NULL, then use 'cols' to determine layoutif (is.null(layout)) {# Make the panel# ncol: Number of columns of plots# nrow: Number of rows needed, calculated from # of colslayout <- matrix(seq(1, cols * ceiling(numPlots/cols)),ncol = cols, nrow = ceiling(numPlots/cols))}#print(layout)if (numPlots==1) {print(plots[[1]])} else {# Set up the pagegrid.newpage()pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))#print(plots) # Make each plot, in the correct locationfor (i in 1:numPlots) {# Get the i,j matrix positions of the regions that contain this subplotmatchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,layout.pos.col = matchidx$col))}} }

轉載于:https://www.cnblogs.com/bregman/p/6031098.html

總結

以上是生活随笔為你收集整理的ggplot2画图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。