当 data.frame 读取为非数字时,如何在条形图上记录刻度 y 轴? R

How to log scale y axis on bar chart when data.frame reads as non-numeric? R

我对来自 barplot()

的参数 log = "y" 有疑问
        ADR BCLREQ CALL  BOND FUT     CD   CDS CDX 
IO Only    "0"   "3"         "0"      "0"  "0" "1" 
IO and TS  "0"   "0"         "0"      "0"  "0" "0" 
No Changes "5"   "45"        "9"      "39" "2" "11"
TS Only    "0"   "0"         "0"      "0"  "0" "0" 
Freq       "5"   "48"        "9"      "39" "2" "12".  

.

 > dput(data)
structure(c("0", "0", "5", "0", "5", "3", "0", "45", "0", "48", 
"0", "0", "9", "0", "9", "0", "0", "39", "0", "39", "0", "0", 
"2", "0", "2", "1", "0", "11", "0", "12"), .Dim = 5:6, .Dimnames = list(
    c("IO Only", "IO and TS", "No Changes", "TS Only", "Freq"
    ), c("ADR", "BCLREQ CALL", "BOND FUT", "CD", "CDS", "CDX"
    )))

现在条形图

  BAR2 <- barplot(data[1:4,], main = "Build Efficiency", ylab = "Manual Frequency", 
                              xlab = "IO Screen", beside = F, log = "y",
                              col = c("blue", "green", "red", "yellow"), las = 1, legend.text = rownames(data)[1:4])

以上结果应该是:

我想以对数刻度制作 'Y' 轴。添加 log = "y", 后我得到:

Error in height + offset : non-numeric argument to binary operator

经过一番挖掘,我意识到我的数据可能不是数字?不确定是否检查正确但是:

> str(data[1,2])
 chr "3"

所以我尝试了:

  data <- data.matrix(data, rownames.force = NA)

  data[1:5,] <- sapply(data[1:5,], as.numeric)

我仍然得到:

Error in height + offset : non-numeric argument to binary operator

如评论所述,您的数据是以字符形式存储的。由于 data 是矩阵,因此使用 mode(data) = "numeric"

您的数据集中也有 0 的问题,如果不进行转换,您将无法对它进行日志转换。请参阅此 post 以获取一些解决方案:https://stats.stackexchange.com/questions/1444/how-should-i-transform-non-negative-data-including-zeros