简体   繁体   English

从 mvbutils R-package 控制食物网图中的图布局

[英]Control plot layout in foodweb plot from mvbutils R-package

I would like to visualize how functions in my own R package depend on each other.我想可视化我自己的 R 包中的函数如何相互依赖。 For this purpose I use the foodweb() function from the mvbutils package.为此,我使用mvbutils包中的foodweb()函数。

I can get the right functional dependencies out without a problem but the plot looks a bit messy, with lines crossing each other and function names not aligned vertically or horizontally.我可以毫无问题地获得正确的函数依赖关系,但该图看起来有点混乱,线条相互交叉,函数名称未垂直或水平对齐。

Is there a way to control the layout of the plot similar to the way this works in the igraph package?有没有办法控制绘图的布局,类似于igraph包中的工作方式?

Example例子

dirPath <- "~/dev/stackoverflow/46910042"
setwd(dirPath)

## Download example Package
urlPackage <- "https://github.com/kbroman/qtlcharts/archive/master.zip"
download.file(urlPackage, destfile = "master.zip")
unzip("./master.zip", exdir = dirPath, overwrite = TRUE)

## Install or load mcbutils
if (!require(mvbutils)) install.packages("mvbutils")
thefiles = list.files(path = "./qtlcharts-master/R/", full.names = TRUE)
thefiles
## Now we load all the package files into memory, so we can have 
## foodweb generate a map of the package functions.
sapply(thefiles, source)

## Generate plot 
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)

Plot Output:绘图输出:

在此处输入图片说明

Michael,迈克尔,

One option is to look behind the curtains of foodweb.一种选择是在食物网的幕后看。 The mvbutils::foodweb function returns an object of (S3) class foodweb . mvbutils::foodweb函数返回(S3)类foodweb的对象。 This has three components:这包括三个组成部分:

  • funmat a matrix of 0s and 1s showing what (row) calls what (column). funmat一个由 0 和 1组成的矩阵,显示什么(行)调用什么(列)。 The dimnames are the function names. dimnames 是函数名称。
  • x shows the x-axis location of the centre of each function's name in the display, in par("usr") units x显示显示中每个函数名称中心的 x 轴位置,以 par("usr") 为单位
  • level shows the y-axis location of the centre of each function's name in the display, in par("usr") units. level以 par("usr") 为单位显示显示中每个函数名称中心的 y 轴位置。

thus one approach we can take is to call foodweb but tell it not to create a plot rather return a foodweb object.因此,我们可以采取的一种方法是调用foodweb但告诉它不要创建绘图而是返回一个foodweb对象。 This then allows us to manipulate the data directory or via graphics::plot() externally of the defaults provided by the mvbutils::foodweb() function.这允许我们在mvbutils::foodweb()函数提供的默认值的外部操作数据目录或通过graphics::plot()

Why?为什么? Well, to do what you suggest my sense is three options exist:好吧,按照你的建议,我的感觉是存在三种选择:

  • You can either play with mvbutils::foodweb() parameters.您可以使用mvbutils::foodweb()参数。
  • Work with data structure returned with another plotting package.使用另一个绘图包返回的数据结构。
  • Use graphics::par() and graphics::plot to manipulate the plot size and attributes of the foodweb structure returned.使用graphics::par()graphics::plot来操作返回的foodweb结构的绘图大小和属性。

It would be great to know your preference.知道您的偏好会很棒。 Excluding, that my sense was to provide a base example:排除,我的感觉是提供一个基本的例子:

Plot Package Example绘图包示例

In the case of using graphics::plot , you need to go look at how you manipulate graphics:par .在使用graphics::plot的情况下,您需要查看如何操作graphics:par par() allows you to set or query graphical parameters. par()允许您设置或查询图形参数。 For example, if we want to clean up the function plot you might choose to modify the grahics::par() fin parameter to increase the figure region dimensions, (width, height), in inches.例如,如果我们想要清理函数图,您可以选择修改grahics::par() fin参数以增加图形区域尺寸(宽度、高度),以英寸为单位。 A simple example but my sense it helps map out and demonstrate the options available to you.一个简单的例子,但我觉得它有助于绘制和演示可用的选项。

## Generate plot
if (!require(qtlcharts)) install.packages("qtlcharts")

## Here we specify `asNamespace` to get the package internals
fw <- foodweb( where = asNamespace( "qtlcharts"),
               plotting = FALSE,
               )
#Display foodweb structure
str(fw)

# Expand plot figure region dimensions...
par(fin = c(9.9,7))
# Plot fw strucuture
plot(fw, 
     border = TRUE, 
     expand.xbox = 1,
     boxcolor = "pink", lwd = 1.5, cex = 0.8)

Plot Output example绘图输出示例

Note that the function names are not spaced out.请注意,函数名称没有间隔开。 Note I cut the top and bottom white of plot here.请注意,我在这里剪切了绘图的顶部和底部白色。 In this case, you can play with the par constraints such as margin to get the plot you want.在这种情况下,您可以使用标准约束(例如边距)来获得所需的绘图。

在此处输入图片说明

Pruning your plot修剪你的情节

Another option within the constraints of mvbutils::foodweb is to use the prune and rprune option to simplify your plots. mvbutils::foodweb约束中的另一个选项是使用prunerprune选项来简化您的绘图。 These are super poweful and useful especially the regular expression version.这些是超级强大和有用的,尤其是正则表达式版本。

if (!require(qtlcharts)) install.packages("qtlcharts")
fw <- foodweb( where = asNamespace( "qtlcharts"),
               plotting = FALSE)

str(fw)
par(fin = c(9.9,7))
plot(fw, 
     border = TRUE, 
     expand.xbox = 1,
     boxcolor = "pink", lwd = 1.5, cex = 0.8)

fw <- foodweb( where = asNamespace( "qtlcharts"),
               rprune = "convert_", ## search on `convert_` to negate use `~convert_`
               plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw, 
     border = TRUE, 
     expand.xbox = 1,
     boxcolor = "pink", lwd = 1.5, cex = 0.8)

在此处输入图片说明

Hoping the above information points you in the right direction.希望以上信息为您指明正确的方向。 T. T。

Because of the fact that there are many data, connections etc, the plot is squeezed in order to fit in the screen, hence it becomes messy.因为有很多数据、连接等,为了适应屏幕,情节被挤压,因此变得凌乱。

What I would suggest is to save it in a PDF or PNG with big enough width and Height and then you can zoom in. This will save you a lot of time.我的建议是将其保存为具有足够大的宽度和高度的 PDF 或 PNG,然后您可以放大。这将为您节省大量时间。 EG例如

## Generate plot 
pdf( "mygraph.pdf", width = 50, height = 80 )
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
dev.off()

In addition, you can play with the plot options of foodweb .此外,您可以使用foodweb的情节选项。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM