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

时间:2017-09-29 14:14:48

标签: r plot character bar-chart numeric

我遇到来自log = "y"

的论据barplot()的问题
        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"
    )))

现在是barplot

  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])

以上应导致: enter image description here

我想制作&#39; Y&#39;轴为对数刻度。添加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

1 个答案:

答案 0 :(得分:0)

如上所述,您的数据存储为字符。由于data是矩阵,因此请使用mode(data) = "numeric"

您还遇到数据集中存在0的问题,如果没有转换,则无法记录转换。有关解决方案,请参阅此帖子:https://stats.stackexchange.com/questions/1444/how-should-i-transform-non-negative-data-including-zeros