My actual data set is quite large and it takes R some time to process it. So I wrote a little C program to compute the frequency for each possible value. (Say, the possible values in the data set are 0,1,2,3
.) So I have a frequency distribution which (for the sake of presentation) looks like this:
我的实际数据集非常大,需要R一段时间来处理它。所以我写了一个小C程序来计算每个可能值的频率。 (比方说,数据集中的可能值是0,1,2,3。)所以我的频率分布(为了演示)看起来像这样:
0.1 0.4 0.3 0.2
If I feed this data to ggplot2
using geom_histogram
, I don't get the right histogram. So how can I draw a histogram with the above frequency distribution?
如果我使用geom_histogram将这些数据提供给ggplot2,我得不到正确的直方图。那么如何绘制具有上述频率分布的直方图?
2 个解决方案
#1
#2
0
My approach without creating an extra data frame. In the x axis you can find the number of your frequencies
我的方法没有创建额外的数据框架。在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")
#1
3
You will want to use stat = 'identity'
within the geom_bar
call.
您将需要在geom_bar调用中使用stat ='identity'。
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")
#2
0
My approach without creating an extra data frame. In the x axis you can find the number of your frequencies
我的方法没有创建额外的数据框架。在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")