【问题标题】:How can I plot a histogram with read_excel and barplot?如何使用 read_excel 和 barplot 绘制直方图?
【发布时间】:2021-05-14 08:07:27
【问题描述】:

我有一个名为 crime.xlsx 的 Excel 工作表,其中包含 X-Age 和 Y-Frequency。我希望它显示每个年龄段发生了多少次犯罪。

在 RStudio 中,我创建了一个新的 R Markdown,现在我想使用 read_excel 和 barplot 来绘制数据。

library("readxl")
res <- read_excel("crime.xlsx", sheet = 1)
head(res[, 1:2])
barplot(res)

这给了我以下错误:

barplot.default(res) 中的错误:“高度”必须是向量或矩阵 调用:匿名 withVisible -> eval -> eval -> barplot -> barplot.default 另外:警告消息:已构建包 readxl 在 R 版本 4.0.5 下

我正在跑步 Windows 10 与 RStudio 1.4。

【问题讨论】:

    标签: r


    【解决方案1】:

    可以使用barplot的公式界面:

    res <- readxl::read_excel("crime.xlsx", sheet = 1)
    barplot(Y~X, res)
    

    【讨论】:

    • eval(predvars, data, env) 中的错误:找不到对象 Y 调用:匿名 ... eval -> 匿名 - model.fram.default -> eval -> eval
    • @Europa 将XY 替换为res 中的确切列名。如果您仍然收到错误消息,请将dput(head(res[, 1:2])) 添加到您的帖子中。
    • 但是@Ronak Shah 我该怎么做呢?我只使用 R 6 个月。
    • 这行得通:barplot(Y - Frequency~X - Age, res)
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多