如何操作R varImpPlot中的y轴文本标签?

时间:2022-10-19 09:30:25

The following sample resembles my dataset:

以下示例类似于我的数据集:

require(randomForest)

alpha = c(1,2,3,4,5,6)
bravo = c(2,3,4,5,6,7)
charlie = c(2,6,5,3,5,6)
mydata = data.frame(alpha,bravo,charlie)

myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE)

varImpPlot(myrf, type = 2)

I cannot seem to control the placement of the y-axis labels in varImpPlot. I have tried altering the plot parameters (e.g. mar, oma), with no success. I need the y-axis labels shifted to the left in order to produce a PDF with proper label placement.

我似乎无法控制varImpPlot中y轴标签的位置。我尝试改变绘图参数(例如mar,oma),但没有成功。我需要将y轴标签向左移动,以便生成具有适当标签放置的PDF。

How can I shift the y-axis labels to the left?

如何将y轴标签向左移动?

3 个解决方案

#1


7  

I tried to use adj parameter but it produces a bug. As varImpPlot , use dotchart behind, Here a version using lattice dotplot. Then you can customize you axs using scales parameters.

我试图使用adj参数,但它产生了一个bug。作为varImpPlot,使用dotchart后面,这里有一个使用grid dotplot的版本。然后,您可以使用比例参数自定义您的斧头。

imp <- importance(myref, class = NULL, scale = TRUE, type = 2)
dotplot(imp, scales=list(y =list(cex=2,
                                       at = c(1,2),
                                       col='red',
                                       rot =20,
                                       axs='i') ,
                               x =list(cex=2,col='blue')) )

如何操作R varImpPlot中的y轴文本标签?

#2


4  

You can extract the data needed to construct the plot out of myref and construct a plot with ggplot. By doing so you have more freedom in tweaking the plot. Here are some examples

您可以从myref中提取构建绘图所需的数据,并使用ggplot构建绘图。通过这样做,你可以更*地调整情节。这里有些例子

library(ggplot2)

str(myrf)
str(myrf$importance)
data <- as.data.frame(cbind(rownames(myrf$importance),round(myrf$importance[,"IncNodePurity"],1)))
colnames(data) <- c("Parameters","IncNodePurity")
data$IncNodePurity <- as.numeric(as.character(data$IncNodePurity))

Standard plot:

(p <- ggplot(data) + geom_point(aes(IncNodePurity,Parameters))) 

Rotate y-axis labels:

旋转y轴标签:

(p1 <- p+ theme(axis.text.y = element_text(angle = 90, hjust = 1)))

Some more tweaking (also first plot shown here):

一些更多的调整(这里显示的第一个图):

(p2 <- p1 +  scale_x_continuous(limits=c(3,7),breaks=3:7) + theme(axis.title.y = element_blank())) 

Plot that looks like the varImpPlot (second plot shown here) :

看起来像varImpPlot的绘图(此处显示的第二个图):

(p3 <- p2+ theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(colour = 'gray', linetype = 'dashed'),
panel.background = element_rect(fill='white', colour='black')))

Saving to pdf is easy with ggplot:

使用ggplot可以轻松保存为pdf:

ggsave("randomforestplot.pdf",p2) 

or

ggsave("randomforestplot.png",p2)

p2

如何操作R varImpPlot中的y轴文本标签?

p3

如何操作R varImpPlot中的y轴文本标签?

#3


1  

Did I understood correctly, that you want to get texts charlie and bravo more left of the boundary of the plot? If so, here's one hack to archive this, based on the modification of the rownames used in plotting:

我是否理解正确,你想让文本查理和布拉沃更多地留在情节的边界?如果是这样,根据绘图中使用的rownames的修改,这里有一个hack来存档:

myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE)
#add white spaces at the end of the rownames
rownames(myrf$importance)<-paste(rownames(myrf$importance), "   ") 
varImpPlot(myrf, type = 2)

The adj parameter in dotchart is fixed as 0 (align to right), so that cannot be changed without modifying the code of dotchart:

dotchart中的adj参数固定为0(右对齐),因此在不修改dotchart代码的情况下无法更改:

mtext(labs, side = 2, line = loffset, at = y, **adj = 0**, col = color, 
    las = 2, cex = cex, ...)

(from dotchart)

EDIT: You can make another type of hack also. Take the code of dotchart, change the above line to

编辑:您也可以制作另一种类型的黑客。拿点图的代码,将上面的行改为

 mtext(labs, side = 2, line = loffset, at = y, adj = adjust_ylab, col = color, 
        las = 2, cex = cex, ...)

Then add argument adjust_ylab to the argument list, and rename the function as for example dotchartHack. Now copy the code of varImpPlot, find the line which calls dotchart, change the function name to dotchartHack and add the argument adjust_ylab=adjust_ylab to function call, rename the function to varImpPlotHack and add adjust_ylab to this functions argument list. Now you can change the alignment of the charlie and bravo by changing the parameter adjust_ylab:

然后将参数adjust_ylab添加到参数列表中,并重命名该函数,例如dotchartHack。现在复制varImpPlot的代码,找到调用dotchart的行,将函数名更改为dotchartHack,并将参数adjust_ylab = adjust_ylab添加到函数调用,将函数重命名为varImpPlotHack并将adjust_ylab添加到此函数参数列表中。现在,您可以通过更改参数adjust_ylab来更改charlie和bravo的对齐方式:

 myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE) 
 varImpPlotHack(myrf, type = 2,adjust_ylab=0.5)

From ?par:

The value of adj determines the way in which text strings are justified in text, mtext and title. A value of 0 produces left-justified text, 0.5 (the default) centered text and right-justified text. (Any value in [0, 1] is allowed, and on most devices values outside that interval will also work.)

adj的值确定文本字符串在text,mtext和title中的对齐方式。值为0会生成左对齐文本,0.5(默认)居中文本和右对齐文本。 (允许[0,1]中的任何值,并且在大多数设备上,该间隔之外的值也将起作用。)

#1


7  

I tried to use adj parameter but it produces a bug. As varImpPlot , use dotchart behind, Here a version using lattice dotplot. Then you can customize you axs using scales parameters.

我试图使用adj参数,但它产生了一个bug。作为varImpPlot,使用dotchart后面,这里有一个使用grid dotplot的版本。然后,您可以使用比例参数自定义您的斧头。

imp <- importance(myref, class = NULL, scale = TRUE, type = 2)
dotplot(imp, scales=list(y =list(cex=2,
                                       at = c(1,2),
                                       col='red',
                                       rot =20,
                                       axs='i') ,
                               x =list(cex=2,col='blue')) )

如何操作R varImpPlot中的y轴文本标签?

#2


4  

You can extract the data needed to construct the plot out of myref and construct a plot with ggplot. By doing so you have more freedom in tweaking the plot. Here are some examples

您可以从myref中提取构建绘图所需的数据,并使用ggplot构建绘图。通过这样做,你可以更*地调整情节。这里有些例子

library(ggplot2)

str(myrf)
str(myrf$importance)
data <- as.data.frame(cbind(rownames(myrf$importance),round(myrf$importance[,"IncNodePurity"],1)))
colnames(data) <- c("Parameters","IncNodePurity")
data$IncNodePurity <- as.numeric(as.character(data$IncNodePurity))

Standard plot:

(p <- ggplot(data) + geom_point(aes(IncNodePurity,Parameters))) 

Rotate y-axis labels:

旋转y轴标签:

(p1 <- p+ theme(axis.text.y = element_text(angle = 90, hjust = 1)))

Some more tweaking (also first plot shown here):

一些更多的调整(这里显示的第一个图):

(p2 <- p1 +  scale_x_continuous(limits=c(3,7),breaks=3:7) + theme(axis.title.y = element_blank())) 

Plot that looks like the varImpPlot (second plot shown here) :

看起来像varImpPlot的绘图(此处显示的第二个图):

(p3 <- p2+ theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(colour = 'gray', linetype = 'dashed'),
panel.background = element_rect(fill='white', colour='black')))

Saving to pdf is easy with ggplot:

使用ggplot可以轻松保存为pdf:

ggsave("randomforestplot.pdf",p2) 

or

ggsave("randomforestplot.png",p2)

p2

如何操作R varImpPlot中的y轴文本标签?

p3

如何操作R varImpPlot中的y轴文本标签?

#3


1  

Did I understood correctly, that you want to get texts charlie and bravo more left of the boundary of the plot? If so, here's one hack to archive this, based on the modification of the rownames used in plotting:

我是否理解正确,你想让文本查理和布拉沃更多地留在情节的边界?如果是这样,根据绘图中使用的rownames的修改,这里有一个hack来存档:

myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE)
#add white spaces at the end of the rownames
rownames(myrf$importance)<-paste(rownames(myrf$importance), "   ") 
varImpPlot(myrf, type = 2)

The adj parameter in dotchart is fixed as 0 (align to right), so that cannot be changed without modifying the code of dotchart:

dotchart中的adj参数固定为0(右对齐),因此在不修改dotchart代码的情况下无法更改:

mtext(labs, side = 2, line = loffset, at = y, **adj = 0**, col = color, 
    las = 2, cex = cex, ...)

(from dotchart)

EDIT: You can make another type of hack also. Take the code of dotchart, change the above line to

编辑:您也可以制作另一种类型的黑客。拿点图的代码,将上面的行改为

 mtext(labs, side = 2, line = loffset, at = y, adj = adjust_ylab, col = color, 
        las = 2, cex = cex, ...)

Then add argument adjust_ylab to the argument list, and rename the function as for example dotchartHack. Now copy the code of varImpPlot, find the line which calls dotchart, change the function name to dotchartHack and add the argument adjust_ylab=adjust_ylab to function call, rename the function to varImpPlotHack and add adjust_ylab to this functions argument list. Now you can change the alignment of the charlie and bravo by changing the parameter adjust_ylab:

然后将参数adjust_ylab添加到参数列表中,并重命名该函数,例如dotchartHack。现在复制varImpPlot的代码,找到调用dotchart的行,将函数名更改为dotchartHack,并将参数adjust_ylab = adjust_ylab添加到函数调用,将函数重命名为varImpPlotHack并将adjust_ylab添加到此函数参数列表中。现在,您可以通过更改参数adjust_ylab来更改charlie和bravo的对齐方式:

 myrf = randomForest(alpha~bravo+charlie, data = mydata, importance = TRUE) 
 varImpPlotHack(myrf, type = 2,adjust_ylab=0.5)

From ?par:

The value of adj determines the way in which text strings are justified in text, mtext and title. A value of 0 produces left-justified text, 0.5 (the default) centered text and right-justified text. (Any value in [0, 1] is allowed, and on most devices values outside that interval will also work.)

adj的值确定文本字符串在text,mtext和title中的对齐方式。值为0会生成左对齐文本,0.5(默认)居中文本和右对齐文本。 (允许[0,1]中的任何值,并且在大多数设备上,该间隔之外的值也将起作用。)