ggExtra绘图格式:不同绘图尺寸的相似边际绘图 [英] ggExtra plot format: similar marginal plots for different plot dimensions

查看:1115
本文介绍了ggExtra绘图格式:不同绘图尺寸的相似边际绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  require(ggplot2)使用ggplot2 + ggExtra生成的格式化图的问题,并且与任何错误无关。 
#>加载所需的软件包:ggplot2
require(ggExtra)
#>加载所需的软件包:ggExtra
p1 < - ggplot(data = mpg,aes(x = cty,y = cty))+
geom_point()+
xlab /加仑))+
ylab(城市驾驶(英里/加仑))

ggMarginal(p = p1,type =boxplot)

该图中的y轴边际图通常与x轴边际图不相似,即2个箱图的宽度不是类似。当我改变绘图尺寸时(在我的情况下,使用RStudio),这个问题变得更加严重。任何建议如何使2个盒子的宽度相似,同时使用不同的绘图尺寸(宽x高)。

我遇到类似的问题,与其他边缘绘图类型选项ggExtra软件包:直方图,密度。

解决方案

我建议cowplot包中的 axis_canvas 函数。 (免责声明:我是软件包作者。)它需要多一点工作,但它允许你绘制任何你想要的边缘。您可以准确地指定尺寸,以输出单位(例如英寸)为单位。

  require(cowplot)

pmain < - ggplot(data = mpg,aes(x = cty,y = hwy))+
geom_point()+
xlab(City driving(miles / gallon))+
ylab(Highway driving(miles / gallon))

xbox < - axis_canvas(pmain,axis =x,coord_flip = TRUE)+
geom_boxplot(data = mpg,aes(y = cty,x = 1))+ coord_flip()
ybox < - axis_canvas(pmain,axis =y)+
geom_boxplot(data = mpg,aes(y = h-1,x = 1))

p1 < - insert_xaxis_grob(pmain,xbox,grid :: unit(1,in),position =top)
p2 < - insert_yaxis_grob(p1,ybox,grid :: unit(1,in),position =right)
ggdraw(p2)

了解箱形图在以下两幅不同宽高比图像中如何保留其宽度/高度。 (不幸的是Stackoverflow会重新调整图像的大小,所以效果有点模糊,但是你可以看到顶部盒子的高度总是等于边框的宽度。)







第二个优点是因为你可以使用全面的ggplot2来绘制你的边缘图,你可以绘制任何你想要的东西,例如

  require(cowplot)

pmain < - ggplot(data = mpg, aes(x = cty,y = hwy,color = factor(cyl)))+
geom_point()+
xlab(City driving(miles / gallon))+
ylab 高速公路行驶(英里/加仑))+
theme_minimal()

xbox < - axis_canvas(pmain,axis =x,coord_flip = TRUE)+
geom_boxplot (data = mpg,aes(y = cty,x = factor(cyl),color = factor(cyl)))+
scale_x_discrete()+ coord_flip()
ybox < - axis_canvas(pmain, ()= b)b

geom_boxplot(data = mpg,aes(y = hwy,x = factor(cyl),color = factor(cyl))+
scale_x_discrete()
p1< - insert_xaxis_grob(pmain,xbox,grid :: unit(1,in),position =top)
p2 < - insert_yaxis_grob(p1,ybox,grid :: unit (1,in),position =right)
ggdraw(p2)


This question in regarding formatting plots produced using ggplot2 + ggExtra and is not related to any bug.

require(ggplot2)
#> Loading required package: ggplot2
require(ggExtra)
#> Loading required package: ggExtra
p1 <- ggplot(data = mpg,aes(x = cty,y = cty)) + 
  geom_point()+ 
  xlab("City driving (miles/gallon)") +
  ylab("City driving (miles/gallon)")

ggMarginal(p = p1,type= "boxplot")

The y-axis marginal plot in this chart is usually not similar to the x-axis marginal plot i.e. the width of the 2 boxplots are not similar. This problem become more acute when I change plot dimensions (in my case, using RStudio). Any suggestions how to make the width of the 2 boxplots similar while using different plot dimensions (width x height).

I face similar problems with other marginal plot type options provided by ggExtra package: histogram, density.

解决方案

I suggest the axis_canvas function from the cowplot package. (Disclaimer: I'm the package author.) It requires a little more work, but it allows you to draw any marginals you want. And you can specify the size exactly, in output units (e.g. inch).

require(cowplot)

pmain <- ggplot(data = mpg, aes(x = cty, y = hwy)) + 
  geom_point() + 
  xlab("City driving (miles/gallon)") +
  ylab("Highway driving (miles/gallon)")

xbox <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = 1)) + coord_flip()
ybox <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = 1))

p1 <- insert_xaxis_grob(pmain, xbox, grid::unit(1, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox, grid::unit(1, "in"), position = "right")
ggdraw(p2)

See how the boxplots retain their width/height in the following two images with different aspect ratios. (Unfortunately Stackoverflow rescales the images, so the effect is somewhat obscured, but you can see that the height of the top boxplot is always equal to the width of the side one.)

The second advantage is that because you can use full-blown ggplot2 for your marginal plots, you can draw anything you want, e.g. grouped box plots.

require(cowplot)

pmain <- ggplot(data = mpg, aes(x = cty, y = hwy, color = factor(cyl))) + 
  geom_point() + 
  xlab("City driving (miles/gallon)") +
  ylab("Highway driving (miles/gallon)") +
  theme_minimal()

xbox <- axis_canvas(pmain, axis = "x", coord_flip = TRUE) + 
  geom_boxplot(data = mpg, aes(y = cty, x = factor(cyl), color = factor(cyl))) + 
  scale_x_discrete() + coord_flip()
ybox <- axis_canvas(pmain, axis = "y") + 
  geom_boxplot(data = mpg, aes(y = hwy, x = factor(cyl), color = factor(cyl))) +
  scale_x_discrete()

p1 <- insert_xaxis_grob(pmain, xbox, grid::unit(1, "in"), position = "top")
p2 <- insert_yaxis_grob(p1, ybox, grid::unit(1, "in"), position = "right")
ggdraw(p2)

这篇关于ggExtra绘图格式:不同绘图尺寸的相似边际绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆