R ggplot2 问题 - 使用因素

发布于 2024-08-18 05:48:34 字数 580 浏览 1 评论 0原文

我有一个看起来像这样的数据集......

矿吨周
AA 112 41
AA 114 41
AA 119 41
BB 108 41 
BB 112 41
AA 110 42
AA 109 42
AA 102 43
AA 101 43

我想在 ggplot2 中创建一个箱线图来显示每周的吨数分布。但我只想要 AA 的结果。

我以为它会像这样工作......

qplot(factor(week), tonnes[mine == "AA"], data = sql_results, geom = "boxplot")

但相反,我收到了这个错误。

Error in data.frame(x = c(13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,  :

  arguments imply differing number of rows: 423100, 109436

这可能非常简单,但我没有太多运气找到正确的方法来做到这一点。

I've got a dataset that looks like this...

mine tonnes week
AA   112    41
AA   114    41
AA   119    41
BB   108    41 
BB   112    41
AA   110    42
AA   109    42
AA   102    43
AA   101    43

And I want to create a boxplot in ggplot2 to show the distribution of tonnes for each week. But I only want results from mine AA.

I thought it would work like this....

qplot(factor(week), tonnes[mine == "AA"], data = sql_results, geom = "boxplot")

But instead, I get this error.

Error in data.frame(x = c(13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,  :

  arguments imply differing number of rows: 423100, 109436

It's probably dead simple, but I'm not having much luck figuring the right way to do this.

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

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

发布评论

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

评论(1

傻比既视感 2024-08-25 05:48:34

关闭。在您的示例中,您创建了吨的子集,但不是周的子集。

sql_results<-structure(list(mine = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 
1L, 1L), .Label = c("AA", "BB"), class = "factor"), tonnes = c(112, 
114, 119, 108, 112, 110, 109, 102, 101), week = c(41, 41, 41, 
41, 41, 42, 42, 43, 43)), row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9"), .Names = c("mine", "tonnes", "week"), class = "data.frame")

qplot(factor(week), tonnes, data = subset(sql_results,mine=="AA"), geom = "boxplot")

close. In your example you created a subset of tonnes, but not of week.

sql_results<-structure(list(mine = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 
1L, 1L), .Label = c("AA", "BB"), class = "factor"), tonnes = c(112, 
114, 119, 108, 112, 110, 109, 102, 101), week = c(41, 41, 41, 
41, 41, 42, 42, 43, 43)), row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9"), .Names = c("mine", "tonnes", "week"), class = "data.frame")

qplot(factor(week), tonnes, data = subset(sql_results,mine=="AA"), geom = "boxplot")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文