使用 ggplot2 和已制表数据的分组条形图

发布于 2024-09-07 21:35:44 字数 1332 浏览 3 评论 0原文

我将计数模型拟合到实际数据向量,现在想将实际数据和预测数据绘制为分组(躲闪)条形图。由于这是计数模型,因此数据是离散的(X=x 从 0 到 317)。由于我正在拟合模型,因此我只有已列出的预测值数据。

这是我的原始数据框的外观:

  actual predicted
1   3236 3570.4995
2   1968 1137.1202
3    707  641.8186
4    302  414.8763
5    185  285.1854
6    104  203.0502

我转换了要使用 ggplot2 绘制的数据:

melted.data <- melt(plot.data)
melted.data$realization <- c(rep(0:317, times=2))
colnames(melted.data)=c('origin','count','realization')

这样我的数据框现在看起来像这样:

head(melted.data)
  origin count realization
1 actual  3236           0
2 actual  1968           1
3 actual   707           2
4 actual   302           3
5 actual   185           4
6 actual   104           5
> tail(melted.data)
       origin        count realization
631 predicted 1.564673e-27         312
632 predicted 1.265509e-27         313
633 predicted 1.023552e-27         314
634 predicted 8.278601e-28         315
635 predicted 6.695866e-28         316
636 predicted 5.415757e-28         317

当我尝试绘制它时(同样,我想要实际和预测的计数 - -已经在数据中列出 - 通过离散实现),我给出了这个命令:

ggplot(melted.data, stat="identity", aes(x=realization, fill=origin)) + geom_bar(position="dodge")

然而 ggplot2 似乎不喜欢 stat 参数,因为我没有得到正确的条形高度(这将是变量“计数”)。

有什么想法吗?

谢谢,

罗伯托。

I fit a count model to a vector of actual data and would now like to plot the actual and the predicted as a grouped (dodged) bar chart. Since this is a count model, the data are discrete (X=x from 0 to 317). Since I am fitting a model, I only have already-tabulated data for the predicted values.

Here is how my original data frame looks:

  actual predicted
1   3236 3570.4995
2   1968 1137.1202
3    707  641.8186
4    302  414.8763
5    185  285.1854
6    104  203.0502

I transformed the data to be plotted with ggplot2:

melted.data <- melt(plot.data)
melted.data$realization <- c(rep(0:317, times=2))
colnames(melted.data)=c('origin','count','realization')

So that my data frame now looks like this:

head(melted.data)
  origin count realization
1 actual  3236           0
2 actual  1968           1
3 actual   707           2
4 actual   302           3
5 actual   185           4
6 actual   104           5
> tail(melted.data)
       origin        count realization
631 predicted 1.564673e-27         312
632 predicted 1.265509e-27         313
633 predicted 1.023552e-27         314
634 predicted 8.278601e-28         315
635 predicted 6.695866e-28         316
636 predicted 5.415757e-28         317

When I try to graph it (again, I'd like to have the actual and predicted count --which is already tabulated in the data-- by discrete realization), I give this command:

ggplot(melted.data, stat="identity", aes(x=realization, fill=origin)) + geom_bar(position="dodge")

Yet it seems like the stat parameter is not liked by ggplot2, as I don't get the correct bar height (which would be those of the variable "count").

Any ideas?

Thanks,

Roberto.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2024-09-14 21:35:44

如果您使用 stat_identitycount 列),则需要 aes 映射中的 y 值。尝试以下操作:

ggplot(melted.data, aes(x=realization, y=count, fill=origin)) + 
       stat_identity(position="dodge", geom="bar")

ggplot(melted.data, aes(x=realization, y=count, fill=origin)) + 
       geom_bar(position="dodge", stat="identity")

You need y-values in the aes mapping if you use stat_identity (column count). Try the following:

ggplot(melted.data, aes(x=realization, y=count, fill=origin)) + 
       stat_identity(position="dodge", geom="bar")

or

ggplot(melted.data, aes(x=realization, y=count, fill=origin)) + 
       geom_bar(position="dodge", stat="identity")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文