完美对齐几个地块

编程入门 行业动态 更新时间:2024-10-13 08:25:35
本文介绍了完美对齐几个地块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的目标是将散点图和2个密度估计图相结合的复合图。我面临的问题是由于密度图的缺失轴标记和散点图的图例,密度图与散点图没有正确对齐。可以通过 plot.margin 进行调整。但是,这不是一个可取的解决方案,因为如果对图进行更改,我将不得不一遍又一遍地调整它。有没有办法以一种方式定位所有的地块,以便实际的绘图板完美对齐?

我试着将代码保持为最小尽可能,但为了重现这个问题,它仍然相当多。

library(ggplot2) library(gridExtra) df< - data。帧(y = c(rnorm(50,1,1),rnorm(50,-1,1)),x = c(rnorm(50,1,1),rnorm(50,-1,1 )), group = factor(c(rep(0,50),rep(1,50)))) 空< - ggplot()+ geom_point(aes(1,1),color =white)+ theme( plot.background = element_blank(), panel.grid.major = element_blank() , panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank (), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis .ticks = element_blank()) scatter< - ggplot(df,aes(x = x,y = y,color = group))+ geom_point()+ theme legend.position =bottom) top_plot< - ggplot(df,aes(x = y))+ geom_density(alpha = .5,mapping = aes(fill = group ))+ theme(legend.position =none)+ theme(axis.title.y = element_blank(), axis.title.x = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank(), axis.ticks = element_blank()) right_plot< - ggplot(df,aes(x = x))+ geom_density(alpha = .5,mapping = aes(fill = group))+ coord_flip()+ theme(legend.position =none )+ theme(axis.title.y = element_blank(), axis.title.x = element_blank(), axis.text.y = element_blank(), axis.text.x = element_blank(), axis.ticks = element_blank()) $ b $ grid.arrange(top_plot,empty,scatter,right_plot, ncol = 2,nrow = 2,widths = c(4,1),heights = c(1,4))

解决方案

使用

我用 ggplotGrob(right_plot)$ grobs [[4]] 手动选择面板 grob,但当然也可以自动执行此操作。

还有其他选择: ggplot2中有边缘直方图的散点图

My aim is a compounded plot which combines a scatterplot and 2 plots for density estimates. The problem I'm facing is that the density plots do not align correctly with the scatter plot due to the missing axes labeling of the density plots and the legend of the scatter plot. It could be adjusted by playing arround with plot.margin. However, this would not be a preferable solution since I would have to adjust it over and over again if changes to the plots are made. Is there a way to position all plots in a way so that the actual plotting panels align perfectly?

I tried to keep the code as minimal as possible but in order to reproduce the problem it is still quite a lot.

library(ggplot2) library(gridExtra) df <- data.frame(y = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), x = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), group = factor(c(rep(0, 50), rep(1,50)))) empty <- ggplot() + geom_point(aes(1,1), colour="white") + theme( plot.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank() ) scatter <- ggplot(df, aes(x = x, y = y, color = group)) + geom_point() + theme(legend.position = "bottom") top_plot <- ggplot(df, aes(x = y)) + geom_density(alpha=.5, mapping = aes(fill = group)) + theme(legend.position = "none") + theme(axis.title.y = element_blank(), axis.title.x = element_blank(), axis.text.y=element_blank(), axis.text.x=element_blank(), axis.ticks=element_blank() ) right_plot <- ggplot(df, aes(x = x)) + geom_density(alpha=.5, mapping = aes(fill = group)) + coord_flip() + theme(legend.position = "none") + theme(axis.title.y = element_blank(), axis.title.x = element_blank(), axis.text.y = element_blank(), axis.text.x=element_blank(), axis.ticks=element_blank()) grid.arrange(top_plot, empty, scatter, right_plot, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))

解决方案

Using the answer from Align ggplot2 plots vertically to align the plot by adding to the gtable (most likely over complicating this!!)

library(ggplot2) library(gtable) library(grid)

Your data and plots

set.seed(1) df <- data.frame(y = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), x = c(rnorm(50, 1, 1), rnorm(50, -1, 1)), group = factor(c(rep(0, 50), rep(1,50)))) scatter <- ggplot(df, aes(x = x, y = y, color = group)) + geom_point() + theme(legend.position = "bottom") top_plot <- ggplot(df, aes(x = y)) + geom_density(alpha=.5, mapping = aes(fill = group))+ theme(legend.position = "none") right_plot <- ggplot(df, aes(x = x)) + geom_density(alpha=.5, mapping = aes(fill = group)) + coord_flip() + theme(legend.position = "none")

Use the idea from Bapistes answer

g <- ggplotGrob(scatter) g <- gtable_add_cols(g, unit(0.2,"npc")) g <- gtable_add_grob(g, ggplotGrob(right_plot)$grobs[[4]], t = 2, l=ncol(g), b=3, r=ncol(g)) g <- gtable_add_rows(g, unit(0.2,"npc"), 0) g <- gtable_add_grob(g, ggplotGrob(top_plot)$grobs[[4]], t = 1, l=4, b=1, r=4) grid.newpage() grid.draw(g)

Which produces

I used ggplotGrob(right_plot)$grobs[[4]] to select the panel grob manually, but of course you could automate this

There are also other alternatives: Scatterplot with marginal histograms in ggplot2

更多推荐

完美对齐几个地块

本文发布于:2023-05-29 03:38:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   地块   完美

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!