R partykit :: ctree边缘上的偏移标签 [英] R partykit::ctree offset labels on edges

查看:66
本文介绍了R partykit :: ctree边缘上的偏移标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ctree ,并且我的数据集具有创建节点的因素的协变量.该协变量有足够的因素,并且它们的名称足够长,以至于它们在节点处创建的边缘彼此重叠.我想找到一种方法来阻止这种重叠.

我检查了其他问题,发现其中一个我的示例问题的代码:

  libary(partykit)library(tidyverse)#这是下一行mpg数据集的位置.partykit不需要数据(mpg)irt<-ctree(hwy〜as.factor(class),data = mpg)情节(irt) 

所得的第一个节点的一侧为"2人座,紧凑,中型,超小型",另一侧为小型货车,皮卡,SUV".我最终在情节中看到的是"2人座,紧凑型,中型,超小型面包车,皮卡,小型".我已经使图形设备全屏显示.(我还有其他只有一个节点的树,因此它们在全屏尺寸下看起来很奇怪,所以我不想来回走动.)

我的部分解决方案是

  plot(irt,pop = FALSE)seekViewport("edge1-1")grid.text("2人座,紧凑,\ n中型,超小型") 

将"2人座,紧凑型"堆叠在中型,紧凑型"顶部,并使它们不会重叠微型货车,皮卡,SUV".但是现在,我仍然在情节中保留原始的过长标签.我尝试固定的标签所贴的边缘在一个地方折断了,该地方不适用于新的堆叠标签.修复该边缘会很好,但真正的问题是抑制edge1-1上原始的,过长的标签.

解决方案

边缘标签由函数 edge_simple()绘制,该函数为边缘标签提供了各种证明,请参见?edge_simple .仅当边缘标签的平均长度比 justmin 长(默认为 Inf )时,才应用对齐方式(即:无对齐方式).各种理由都是可能的(交替,增加,减少或相等).

因此,在您的情况下,最简单的解决方案可能是将 justmin 设置为足够小的有限值.另外(或另外),您还可以通过设置 gpar(fontsize = ...)来减小字体大小.为了说明,下面的两个示例都是在6英寸x 8英寸PNG设备上生成的:

  library("partykit")data("mpg",package ="ggplot2")irt<-ctree(hwy〜factor(class),data = mpg)情节(irt,ep_args = list(justmin = 15)) 

  plot(irt,ep_args = list(justmin = 15),gp = gpar(fontsize = 10)) 

I am working with ctree and my data set has a covariate of factors that create a node. There are enough factors for that covariate and their names are long enough that they overlap on each other in the edges created at the node. I want to find a way to stop this overlap.

I checked other questions and found one answer that supplies some help. The plot for ctree relies on the grid package and I can use functions to write new labels on the edge. My problem now is that I don't know how to suppress the labels that are printed as default when I plot the tree. I don't know enough about grid or plot.party to figure out which object needs to be suppressed.

An example of my problem in the following image: Code for my example problem:

libary(partykit)
library(tidyverse) #this is here for the mpg data set in next line. not required for partykit
data(mpg)
irt <- ctree(hwy~as.factor(class),data=mpg)
plot(irt)

The resulting 1st node has one edge with "2seater, compact, midsize, subcompact" and the other edge with "minivan, pickup, suv". What I end up seeing in the plot is "2seater, compact, midsize, subcompaminivan, pickup, sub". I've already made the graphics device full screen. (I have other trees that only have one node and so that makes those look odd at the full screen dimension, so I don't want to go back and forth.)

The partial solution I have is

plot(irt, pop=FALSE)
seekViewport("edge1-1")
grid.text("2seater, compact,\n midsize, subcompact")

This stacks "2seater, compact" on top of "midsize, subcompact" and would keep them from overlapping "minivan, pickup, suv". But now, I have the original too-long label still in the plot. And the edge that the label I'm trying fix is attached to has a break in a place that doesn't work with the new stacked label. It would be nice to fix that edge, but the real problem is suppressing the original, too-long label on edge1-1.

解决方案

The edge labels are drawn by the function edge_simple() which offers various kinds of justifications for the edge labels, see ?edge_simple. The justification is only applied if the edge labels are on average longer than justmin, defaulting to Inf (i.e.: no justification). Various justifications are possible (alternating, increasing, decreasing, or equal).

Thus, in your case the simplest solution is probably to set justmin to a small enough finite value. Alternatively (or additionally) you could also decrease the font size by setting gpar(fontsize = ...). For illustration both examples below have been generated on a 6in x 8in PNG device:

library("partykit")
data("mpg", package = "ggplot2")
irt <- ctree(hwy ~ factor(class), data = mpg)
plot(irt, ep_args = list(justmin = 15))

plot(irt, ep_args = list(justmin = 15), gp = gpar(fontsize = 10))

这篇关于R partykit :: ctree边缘上的偏移标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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