首页 > 解决方案 > 计数为 R 的直方图

问题描述

我正在尝试创建我的数据的直方图。我的数据框看起来像这样

x  counts
4  78
5  45
... ...

其中 x 是我想要绘制的变量,counts 是观察次数。如果我做 hist(x) 情节将是误导,因为我没有考虑计数。我也试过:

hist(do.call("c", (mapply(rep, df$x, df$count))))

不幸的是,这不起作用,因为结果向量会太大

sum(df$ount)
[1] 7943571126

我还有其他方法可以尝试吗?

谢谢

标签: rcountbigdatahistogramfrequency

解决方案


解决方案是@Rui Barradas建议的条形图。我ggplot用来绘制数据。

library(ggplot2)
x <- c(4, 5, 6, 7, 8, 9, 10)
counts <- c(78, 45, 50, 12, 30, 50)
df <- data.frame(x=x, counts=counts)

plt <- ggplot(df) + geom_bar(aes(x=x, y=counts), stat="identity")
print(plt)

条形图


推荐阅读