【问题标题】:Creating a bar chart in R from data in a file从文件中的数据在 R 中创建条形图
【发布时间】:2018-06-07 13:27:50
【问题描述】:

首先我创建了两个vectors 并将它们放在一起

Vectors: VektorLHI 和 VektorRHI

sumLHIundRHI<-rbind2(VektorLHI,VektorRHI)

然后我得到:

然后我创建了:

使用命令:

barplot(sumLHIundRHI[,-1])

但是当我尝试设置 beside=TRUE 时,我得到一个错误

barplot(sumLHIundRHI[,-1], beside=TRUE)
Error in -0.01 * height : non-numeric argument to binary Operator

如何正确?

【问题讨论】:

  • 对于每一列(“infact”除外)应该有两个支柱,区分Infact = L和Infakt = R
  • 你能分享一段可重现的代码吗?(stackoverflow.com/help/mcve)
  • 请分享您的数据head

标签: r vector sum bar-chart


【解决方案1】:

假设您已将数据共享为可重现的方式:

sumLHIundRHI_reduced <- data.frame(Infarkt=c("L","R"),
           AG=c(2,5),
           PG=c(4,0),
           pP=c(3,3))

我认为问题在于您的数据结构。特别是数据集的第一列,因为barplot 函数需要一个向量或值矩阵,而您的数据似乎是一个数据框:

data_ordered <- as.matrix(sumLHIundRHI_reduced[,2:4])
row.names(data_ordered) <- levels(sumLHIundRHI_reduced$Infarkt)

现在该功能将正常工作:

barplot(data_ordered, main="Your Plot",
        xlab="Number of ???", col=c("darkblue","red"),
        legend = rownames(data_ordered),beside=TRUE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2017-10-13
    相关资源
    最近更新 更多