r语言 - 如何限制点图的变量



我想创建一个点图,它只包含文本文件中特征的前 10 个值。以下代码有效,但输出是包含所有 160 个变量的点图。

library(lattice)
table<-"imp_s2.txt"
DT<-read.table(table, header=T)
# output graph to pdf file 
pdf("dotplot_s2.pdf")
colnames(DT)
DT$feature <- reorder(DT$feature, DT$IncMSE)
dotplot(feature ~ IncMSE, data = DT,
        aspect = 1.5,
        xlab = "Variable Importance, Scale 2",
        scales = list(cex = .6),
        panel = function (x, y) {
          panel.abline(h = as.numeric(y), col = "gray", lty = 2)
          panel.xyplot(x, as.numeric(y), col = "black", pch = 16)})
dev.off()

如果您包含一个可重现的示例,这将有所帮助。 我的猜测是,这可以通过简单地对数据框进行子集化来完成,以便仅包含具有前 10 个值的行。 这样的东西可能会起作用(尽管我无法测试它):

# get threshold value
cutoff <- sort(DT$IncMSE, decreasing=TRUE)[10]
dotplot(feature ~ IncMSE, 
        data = DT[which(DT$IncMSE>=cutoff),], # this only includes top values
        aspect = 1.5,
        xlab = "Variable Importance, Scale 2",
        scales = list(cex = .6),
        panel = function (x, y) {
          panel.abline(h = as.numeric(y), col = "gray", lty = 2)
          panel.xyplot(x, as.numeric(y), col = "black", pch = 16)})

最新更新