0

我正在尝试使用 R 中的 ggplot2 使用以下数据绘制箱线图:

http://dl.dropbox.com/u/105195130/test.txt

R代码如下:

#!/usr/bin/env Rscript

library(ggplot2)

f<-read.table("test.txt", header=TRUE, sep='\t')

f$Category <- factor(f$Category, levels=unique(as.character(f$Category)))
g<-ggplot(f, aes(Category, Scores)) + stat_boxplot(geom='errorbar', linetype="dashed")
 + geom_boxplot(stat="boxplot", aes(fill=Category)) + geom_vline(linetype="dashed") + 
scale_y_log2()

ggsave("test.pdf")

我收到以下错误:

Error in signif(x, digits) : 
Non-numeric argument to mathematical function

我试图用谷歌搜索错误并通过查看文件的模式和类来验证列“分数”(第二列)是否为非数字。我觉得很好。第一列是因子,第二列是数字。我无法理解我做错了什么。

任何帮助是极大的赞赏。

谢谢!

traceback() 打印以下内容:

19: f(unlist(l[numeric]))
18: get(x, envir = this, inherits = inh)(this, ...)
17: scales$y$labels()
16: get(x, envir = this, inherits = inh)(this, ...)
15: coord$compute_ranges(scales)
14: FUN(1:3[[1L]], ...)
13: lapply(seq_along(data), function(i) {
    layer <- layers[[i]]
    layerd <- data[[i]]
    grobs <- matrix(list(), nrow = nrow(layerd), ncol = ncol(layerd))
    for (i in seq_len(nrow(layerd))) {
        for (j in seq_len(ncol(layerd))) {
            scales <- list(x = .$scales$x[[j]]$clone(), y = .$scales$y[[i]]$clone())
            details <- coord$compute_ranges(scales)
            grobs[[i, j]] <- layer$make_grob(layerd[[i, j]], 
                details, coord)
        }
    }
    grobs
})
12: get(x, envir = this, inherits = inh)(this, ...)
11: facet$make_grobs(data, layers, cs)
10: ggplot_build(plot)
9: ggplotGrob(x, ...)
8: grid.draw(ggplotGrob(x, ...))
7: print.ggplot(plot, keep = keep, drop = drop)
6: print(plot, keep = keep, drop = drop)
5: ggsave("test.pdf") at test.R#10
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("test.R")

我是 R 新手,所以这对我来说没有意义,如果有人能解释我如何使用它来调试我的脚本,那就太好了。

4

1 回答 1

2

我的ggplot2 (0.9.3.1) 版本没有scale_y_log2功能。但这是可行的:

library(scales)
g<-ggplot(tmp, aes(Category, Scores)) + 
    stat_boxplot(geom='errorbar', linetype="dashed")+ 
    geom_boxplot(stat="boxplot", aes(fill=Category)) + 
    geom_vline(linetype="dashed") + 
    scale_y_continuous(trans = log_trans(2))

我相信在过渡到 0.9.0 的过程中, scale_y_log2(连同许多其他 scale 功能)已从 ggplot2中移出并重新放入scales包中。你也许应该升级......?

于 2013-03-28T17:08:49.870 回答