如何重新排列R中矩阵的行

发布于 2025-02-09 13:21:31 字数 985 浏览 1 评论 0原文

我有以下数据框架:

> agg_2
# A tibble: 3 × 3
  bcs        default_flag pred_default
  <chr>             <dbl>        <dbl>
1 high-score      0.00907       0.0121
2 low-score       0.0345        0.0353
3 mid-score       0.0210        0.0204

我使用以下代码将其绘制为条形图:

barplot(t(as.matrix(agg_2[,-1])),
        main = "Actual Default vs Predicted Default",
        xlab = "Score Category",
        ylab = "Default Rate",
        names.arg = c("High Score", "Low Score", "Mid Score"),
        col = gray.colors(2),
        beside = TRUE)
legend("topleft",
       c("Default", "Pred. Default"),
       fill = gray.colors(2))

它给了我这一点:

“

我如何重新安排数据框架/矩阵,以便bar plot中的一组bar如下:低分然后Mid Score然后高分

I have the following data frame:

> agg_2
# A tibble: 3 × 3
  bcs        default_flag pred_default
  <chr>             <dbl>        <dbl>
1 high-score      0.00907       0.0121
2 low-score       0.0345        0.0353
3 mid-score       0.0210        0.0204

I plot it as a bar plot using the following code:

barplot(t(as.matrix(agg_2[,-1])),
        main = "Actual Default vs Predicted Default",
        xlab = "Score Category",
        ylab = "Default Rate",
        names.arg = c("High Score", "Low Score", "Mid Score"),
        col = gray.colors(2),
        beside = TRUE)
legend("topleft",
       c("Default", "Pred. Default"),
       fill = gray.colors(2))

and it gives me this:

Bar plot

How can I rearrange the data frame/matrix so that the pairs of bars in the bar plot are as follows: Low Score then Mid Score then High Score?

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

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

发布评论

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

评论(1

小鸟爱天空丶 2025-02-16 13:21:31

这是一个潜在的解决方案:

agg_2 <- read.table(text = "bcs        default_flag pred_default
high-score      0.00907       0.0121
low-score       0.0345        0.0353
mid-score       0.0210        0.0204", header = TRUE)

agg_2$bcs <- factor(agg_2$bcs, levels = c("low-score", "mid-score", "high-score"), ordered = TRUE)
agg_2 <- agg_2[order(agg_2$bcs),]

barplot(t(as.matrix(agg_2[,-1])),
        main = "Actual Default vs Predicted Default",
        xlab = "Score Category",
        ylab = "Default Rate",
        names.arg = agg_2$bcs,
        col = gray.colors(2),
        beside = TRUE)
legend("topright",
       c("Default", "Pred. Default"),
       fill = gray.colors(2))

“”

在2022-06-21上由 reprex软件包(v2.0.1)

Here is one potential solution:

agg_2 <- read.table(text = "bcs        default_flag pred_default
high-score      0.00907       0.0121
low-score       0.0345        0.0353
mid-score       0.0210        0.0204", header = TRUE)

agg_2$bcs <- factor(agg_2$bcs, levels = c("low-score", "mid-score", "high-score"), ordered = TRUE)
agg_2 <- agg_2[order(agg_2$bcs),]

barplot(t(as.matrix(agg_2[,-1])),
        main = "Actual Default vs Predicted Default",
        xlab = "Score Category",
        ylab = "Default Rate",
        names.arg = agg_2$bcs,
        col = gray.colors(2),
        beside = TRUE)
legend("topright",
       c("Default", "Pred. Default"),
       fill = gray.colors(2))

Created on 2022-06-21 by the reprex package (v2.0.1)

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