我的实际数据集非常大,需要R一段时间来处理它。所以我写了一个小C程序来计算每个可能值的频率。 (比方说,数据集中的可能值是0,1,2,3
。)所以我的频率分布(为了演示)看起来像这样:
0.1 0.4 0.3 0.2
如果我使用ggplot2
将此数据提供给geom_histogram
,我就无法获得正确的直方图。那么如何绘制具有上述频率分布的直方图?
答案 0 :(得分:3)
您需要在stat = 'identity'
来电中使用geom_bar
。
library(ggplot2)
dat <- data.frame(x = c(0, 1, 2, 3), y = c(0.1, 0.4, 0.3, 0.2))
ggplot(dat) +
geom_bar(mapping = aes(x = x, y = y), stat = "identity")
答案 1 :(得分:0)
我的方法没有创建额外的数据框。在x轴上,您可以找到频率数
library(ggplot2)
x<-c(0.1, 0.4, 0.3, 0.2)
ggplot(data.frame(x), aes(y=x, x=1:length(x)))+
geom_bar(stat = "identity")