简体   繁体   English

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

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

I am having issues with the argument log = "y" from barplot() 我在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"
    )))

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

The above should result in: 以上结果将导致: 在此处输入图片说明

I would like to make the 'Y' axis in log scale. 我想将“ Y”轴设为对数刻度。 after adding log = "y", I get: 添加log = "y",我得到:

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

After a bit of digging I realized my data might not be numeric? 经过一番挖掘,我意识到我的数据可能不是数字的吗? not sure if checking correctly but: 不知道是否检查正确,但是:

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

So I tried: 所以我尝试了:

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

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

I still get: 我仍然得到:

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

As the comments mentioned, your data is stored as characters. 如前所述,您的数据存储为字符。 Since data is a matrix, use mode(data) = "numeric" . 由于data是矩阵,因此请使用mode(data) = "numeric"

You also have the problem that 0's are in your dataset and you cannot log transform this without a transformation. 您还存在数据集中0的问题,如果不进行转换就无法进行日志转换。 See this post for some solutions: https://stats.stackexchange.com/questions/1444/how-should-i-transform-non-negative-data-including-zeros 请参阅此帖子以获取一些解决方案: https : //stats.stackexchange.com/questions/1444/how-should-i-transform-non-negative-data-includes-zeros

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM