本文介绍了ggplot2中的双y轴用于多个面板图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! kohske提供了一个解决方案来为单个面板数字添加双y轴( http://rpubs.com/kohske/dual_axis_in_ggplot2 ) @Andreas展示了一个将两个图与ggplot2对齐的例子(仍然是单个面板图)(用2个y轴绘制,一个y轴绘制在左边,另一个右边的y轴) 如果图形有多个面板geom_wrap或geom_grid),例如下面的4个面板图。我想将p1的轴添加到合并图的面板的右侧。 编辑:我在这里删除我的代码并发布我自己的解决方案作为答案。以防万一有人需要它。 解决方案我发布自己的解决方案作为答案,以防万一需要。 库(ggplot2)库(gtable)库(网格) df1< ; - expand.grid(list(x = 1:10,z = 1:4,col = 1:2)) df2 df1 $ y df2 $ y p1 p1 p1 p1 p1 plot.margin =单位(c(1,2,0.5,0.5),'cm ')) p2 p2 p2 < - p2 + facet_wrap(〜z) p2 p2 plot.margin =单位(c(1,2,0.5,0.5),'cm')) g1 g2 combo_grob pos < - 长度(combo_grob) - 1 combo_grob $ grobs [[pos]]< - cbind(g1 $ grobs [[pos]], g2 $ grobs [[pos]],size = 'first') panel_num< - length(unique(df1 $ z)) for(i in seq(panel_num)) {#grid。 ls(g1 $ grobs [[i + 1]]) panel_grob< - getGrob(g1 $ grobs [[i + 1]],'geom_point.points', grep = TRUE,global = TRUE) combo_grob $ grobs [[i + 1]] panel_grob)} pos_a< - grep('axis_l',names(g1 $ grobs)) axis< - g1 $ grobs [pos_a] for(i in seq(along = axis) ) { if(i%in%c(2,4)) { pp ax< - axis [[1]] $ 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.5,cm) ax $ grobs [[2]] $ x combo_grob< - gtable_add_cols(combo_grob,g2 $ widths [g2 $ layout [pos_a [i],] $ l],length(combo_grob $ widths) - 1) combo_grob< - gtable_add_grob (combo_grob,ax,pp $ t,length(combo_grob $ widths)-1,pp $ b)} } pp ia< - 其中(g1 $ layout $ name ==ylab) ga ga $ rot ga $ x combo_grob< - gtable_add_cols(combo_grob,g2 $ widths [g2 $ layout [ia,] $ l],length(combo_grob $ widths) - 1) combo_grob< - gtable_add_grob(combo_grob ,ga,pp $ t,length(combo_grob $ widths)-1,pp $ b) combo_grob $ layout $ clip< - off grid.draw(combo_grob) P1: p> P2: We can not easily create the dual y axis with ggplot2.kohske provided a solution to add dual y axis for single panel figures (http://rpubs.com/kohske/dual_axis_in_ggplot2)@Andreas show an example to align two plots with ggplot2 (still single panel figures) (Plot with 2 y axes, one y axis on the left, and another y axis on the right)These tricks can not work if figures have multiple panels (created by geom_wrap or geom_grid), e.g. 4 panel figure below. I would like to add the axis of p1 to the right side of panels of merged figure.EDIT: I removed my codes here and post my own solution as an answer. Just in case some one needs it. 解决方案 I post my own solution as an answer, just in case some one needs it.library(ggplot2)library(gtable)library(grid)df1 <- expand.grid(list(x = 1:10, z = 1:4, col = 1:2))df2 <- df1df1$y <- df1$x * df1$x + df1$col * 10df2$y <- 4 * df2$x + df1$col * 10p1 <- ggplot(df1)p1 <- p1 + geom_point(aes(x, y, colour = factor(col)))p1 <- p1 + facet_wrap(~z)p1 <- p1 + ylab('Y label for figure 1')p1 <- p1 + theme(legend.position = 'bottom', plot.margin = unit(c(1, 2, 0.5, 0.5), 'cm'))p2 <- ggplot(df2)p2 <- p2 + geom_line(aes(x, y, colour = factor(col)))p2 <- p2 + facet_wrap(~z)p2 <- p2 + ylab('Y label for figure 2')p2 <- p2 + theme(legend.position = 'bottom', plot.margin = unit(c(1, 2, 0.5, 0.5), 'cm'))g1 <- ggplot_gtable(ggplot_build(p1))g2 <- ggplot_gtable(ggplot_build(p2))combo_grob <- g2pos <- length(combo_grob) - 1combo_grob$grobs[[pos]] <- cbind(g1$grobs[[pos]], g2$grobs[[pos]], size = 'first')panel_num <- length(unique(df1$z))for (i in seq(panel_num)){ # grid.ls(g1$grobs[[i + 1]]) panel_grob <- getGrob(g1$grobs[[i + 1]], 'geom_point.points', grep = TRUE, global = TRUE) combo_grob$grobs[[i + 1]] <- addGrob(combo_grob$grobs[[i + 1]], panel_grob)}pos_a <- grep('axis_l', names(g1$grobs))axis <- g1$grobs[pos_a]for (i in seq(along = axis)){ if (i %in% c(2, 4)) { pp <- c(subset(g1$layout, name == paste0('panel-', i), se = t:r)) ax <- axis[[1]]$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.5, "cm") ax$grobs[[2]]$x <- ax$grobs[[2]]$x - unit(1, "npc") + unit(0.8, "cm") combo_grob <- gtable_add_cols(combo_grob, g2$widths[g2$layout[pos_a[i],]$l], length(combo_grob$widths) - 1) combo_grob <- gtable_add_grob(combo_grob, ax, pp$t, length(combo_grob$widths) - 1, pp$b) }}pp <- c(subset(g1$layout, name == 'ylab', se = t:r))ia <- which(g1$layout$name == "ylab")ga <- g1$grobs[[ia]]ga$rot <- 270ga$x <- ga$x - unit(1, "npc") + unit(1.5, "cm")combo_grob <- gtable_add_cols(combo_grob, g2$widths[g2$layout[ia,]$l], length(combo_grob$widths) - 1)combo_grob <- gtable_add_grob(combo_grob, ga, pp$t, length(combo_grob$widths) - 1, pp$b)combo_grob$layout$clip <- "off"grid.draw(combo_grob)P1:P2:Merged: 这篇关于ggplot2中的双y轴用于多个面板图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-29 04:26