本文介绍了在两个不同的(“网格排列”)图之间画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 因此,我设法创建了两个ggplot图,并将它们与 grid.arrange 完全对齐。现在我想连接左图中分布的最小值,1Q,3Q和最大值。我在这两幅图中绘制了水平(红色条纹)线条,其中 geom_hline ,但我想要将这些线条连接起来,因此还要穿过图形之间的空白区域。有什么建议么?以下图表: 哦,是的,复杂的因素:正确的图形有它的坐标翻转! / b> 这是一个可重现的例子: library(ggplot2)库(scale)库(gridExtra) #创建一些数据 df_ahn< - data.frame(yh = rnorm(1000,0.23,0.05)) df_peil< - data.frame(hoogte = rnorm(1000,0,1)) #符合德豪猪 Summary_df< - 汇总(df_ahn $ yh) p_peil geom_histogram(aes(y = cumsum((.. count ..)/ sum(.. count ..) )),binwidth = 0.01,fill =gray)+ stat_bin(aes(y = cumsum((.. count ..)/ sum(.. count ..))),binwidth = 0.01,geom (aes(xintercept = as.vector(Summary_df [1])),lty = 2,color = 2)+ geom_vline(aes(aes()=line,color =black)+ geom_vline xinter (x摘要= as.vector(Summary_df [5])),lty = 2,color = 2)+ geom_vline(aes )+ geom_vline(aes(xintercept = as.vector(Summary_df [6])),lty = 2,color = 2)+ coord_flip()+ ggtitle(Onderschrijdingsfrequentie \ )+ xlab(计量NAP中的Hoogte)+ ylab(Onderschrijdingsfrequentie in%(10%= 36,5 dagen / jaar))+ scale_y_continuous (limit = c(-0,1),labels = percent,breaks = seq(0,1,by = 0.1))+ scale_x_continuous(limits = c(-0.5,0.6),breaks = seq(-0.5 ,0.6,by = 0.1))+ theme(panel.background = element_rect(fill =transparent,color =black), panel.grid.major = element_line(color =darkgray ), panel.grid.minor = element_line(color =gray), strip.background = element_rect(fill =gray), strip.text = element_text(size = 14,color =black), axis.ticks.y = element_line(color =black),a xis.ticks.x = element_line(color =black), axis.text.x = element_text(size = 14,color =black), axis.text.y = element_text( size = 14,color =black), axis.title = element_text(size = 14,color =black)) p_ahn geom_boxplot(outlier.size = 3,outlier.shape = 1)+ geom_hline(aes(yintercept = as.vector(Summary_df [1 ])),lty = 2,color = 2)+ geom_hline(aes(yintercept = as.vector(Summary_df [2])),lty = 2,color = 2)+ geom_hline(aes (yintercept = as.vector(Summary_df [5])),lty = 2,color = 2)+ geom_hline(aes(yintercept = as.vector(Summary_df [6])),lty = 2,color = 2)+ scale_y_continuous(limits = c(-0.5,0.6),breaks = seq(-0.5,0.6,by = 0.1))+ ggtitle(Hoogte groeiplaatsen \\\Kruipend moerascherm)+ ylab()+ xlab()+ 主题(panel.background = element_rect(fill =transparent,color =black), panel .grid.major = element_blank( ), panel.grid.minor = element_blank(), strip.background = element_rect(fill =gray), strip.text = element_text(size = 14,color =黑色), axis.ticks.y = element_line(color =black), axis.ticks.x = element_line(color =white), axis.text。 x = element_text(size = 14,color =white), axis.text.y = element_text(size = 14,color =black), axis.title = element_text(size = 14,color =black,face =bold) ) grid.arrange(p_peil,p_ahn,layout_matrix = matrix(c(1,1,1,2), nrow = 1,byrow = TRUE),ncol = 4) 解决方案您可以从其中一个图表中提取出该行,并将其添加到组合gtable的整个区域, library( ggplot2) library(gtable) library(grid) set.seed(123)y< - rnorm(10) p1< - qplot(1:10,y)+ geom_hline(yintercept = 0,lty = 3) p2 geom_hline(yintercept = 0) #library(gridExtra)#grid.arrange(p1,p2,widths = c(3,1))#no line g1 g2 lines g1 $ grobs [[4]] [[children]] [[3]]< - NULL#删除行 g2 $ grobs [[4]] [[children]]删除行g g $ heights grid.newpage() grid.draw(g) So i have managed to create two ggplot-graphs and plot them perfectly aligned with grid.arrange. Now i would like to connect the min, 1Q, 3Q and max of the boxplot with the distribution in the left graph. I have plotted horizontal (red striped) lines with geom_hline in both graphs, but i would like to have these lines connected, so also go through the white space between the graphs. Any suggestions? Here's the graph:Oh yeah, complicating factor: the right graphs has it's coordinates flipped!Here's a reproducible example:library(ggplot2)library(scales)library(gridExtra)# create some datadf_ahn <- data.frame(yh=rnorm(1000,0.23,0.05))df_peil <- data.frame(hoogte = rnorm(1000,0,1))# vector met de hoogtesSummary_df <- summary(df_ahn$yh)p_peil <- ggplot(df_peil,aes(x=hoogte))+ geom_histogram(aes(y=cumsum((..count..)/sum(..count..))), binwidth = 0.01,fill="gray")+ stat_bin(aes(y=cumsum((..count..)/sum(..count..))),binwidth = 0.01,geom="line",color="black") + geom_vline(aes(xintercept = as.vector(Summary_df[1])), lty = 2,color =2)+ geom_vline(aes(xintercept = as.vector(Summary_df[2])), lty = 2,color =2)+ geom_vline(aes(xintercept = as.vector(Summary_df[5])), lty = 2,color =2)+ geom_vline(aes(xintercept = as.vector(Summary_df[6])), lty = 2,color =2)+ coord_flip() + ggtitle("Onderschrijdingsfrequentie\n waterstand in kreek") + xlab("Hoogte in meter NAP") + ylab("Onderschrijdingsfrequentie in % (10% = 36,5 dagen/jaar)") + scale_y_continuous(limits = c(0, 1),labels = percent, breaks=seq(0,1,by=0.1)) + scale_x_continuous(limits = c(-0.5, 0.6), breaks=seq(-0.5,0.6,by=0.1)) + theme(panel.background = element_rect(fill = "transparent",colour = "black"), panel.grid.major = element_line(colour = "darkgray"), panel.grid.minor = element_line(colour = "gray"), strip.background = element_rect(fill="gray"), strip.text = element_text(size=14, color="black"), axis.ticks.y = element_line(colour = "black"), axis.ticks.x = element_line(colour = "black"), axis.text.x = element_text(size=14, color="black"), axis.text.y = element_text(size=14, color="black"), axis.title = element_text(size=14, color="black") )p_ahn <- ggplot(df_ahn, aes(x=1, y=yh)) + geom_boxplot(outlier.size=3, outlier.shape=1) + geom_hline(aes(yintercept = as.vector(Summary_df[1])), lty = 2,color =2)+ geom_hline(aes(yintercept = as.vector(Summary_df[2])), lty = 2,color =2)+ geom_hline(aes(yintercept = as.vector(Summary_df[5])), lty = 2,color =2)+ geom_hline(aes(yintercept = as.vector(Summary_df[6])), lty = 2,color =2)+ scale_y_continuous(limits = c(-0.5,0.6), breaks=seq(-0.5,0.6,by=0.1)) + ggtitle("Hoogte groeiplaatsen\nKruipend moerascherm") + ylab("") + xlab("") + theme(panel.background = element_rect(fill = "transparent",colour = "black"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), strip.background = element_rect(fill="gray"), strip.text = element_text(size=14, color="black"), axis.ticks.y = element_line(colour = "black"), axis.ticks.x = element_line(colour = "white"), axis.text.x = element_text(size=14, color="white"), axis.text.y = element_text(size=14, color="black"), axis.title = element_text(size=14, color="black", face="bold") )grid.arrange(p_peil,p_ahn, layout_matrix = matrix(c(1,1,1,2), nrow=1, byrow=TRUE), ncol = 4) 解决方案 You could extract the line from one of the plots, and add it to the whole region of the combined gtable,library(ggplot2)library(gtable)library(grid)set.seed(123)y <- rnorm(10)p1 <- qplot(1:10, y) + geom_hline(yintercept=0, lty=3)p2 <- qplot(1:10, 10*y) + geom_hline(yintercept=0)#library(gridExtra)#grid.arrange(p1,p2,widths=c(3,1)) # no lineg1 <- ggplotGrob(p1)g2 <- ggplotGrob(p2)lines <- g1$grobs[[4]][["children"]][[3]]g1$grobs[[4]][["children"]][[3]] <- NULL # remove lineg2$grobs[[4]][["children"]][[3]] <- NULL # remove lineg <- cbind(g1,g2,size="first")g$heights <- unit.pmax(g1$heights, g2$heights)g$widths[[9]] <- unit(1/3, "null")g <- gtable_add_grob(g, lines, l=4, t=3, r=9, z=Inf)grid.newpage()grid.draw(g) 这篇关于在两个不同的(“网格排列”)图之间画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-03 23:27