使用R

发布于 2025-02-05 20:15:26 字数 2032 浏览 1 评论 0 原文

对于示例dataframe df pred_value real_value 分别表示变量的每月预测值和实际值, acc_level 表示预测值的精度级别与通讯月的实际值进行比较,值越少,预测结果就更准确:

df <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L), .Label = c("2022/3/31", "2022/4/30", 
"2022/5/31"), class = "factor"), pred_value = c(2721.8, 2721.8, 
2705.5, 2500, 2900.05, 2795.66, 2694.45, 2855.36, 2300, 2799.82, 
2307.36, 2810.71, 3032.91), real_value = c(2736.2, 2736.2, 2736.2, 
2736.2, 2736.2, 2759.98, 2759.98, 2759.98, 2759.98, 3000, 3000, 
3000, 3000), acc_level = c(1L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 
2L, 3L, 2L, 1L)), class = "data.frame", row.names = c(NA, -13L
))

OUT:

        date pred_value real_value acc_level
1  2022/3/31    2721.80    2736.20         1
2  2022/3/31    2721.80    2736.20         1
3  2022/3/31    2705.50    2736.20         2
4  2022/3/31    2500.00    2736.20         3
5  2022/3/31    2900.05    2736.20         3
6  2022/4/30    2795.66    2759.98         1
7  2022/4/30    2694.45    2759.98         2
8  2022/4/30    2855.36    2759.98         2
9  2022/4/30    2300.00    2759.98         3
10 2022/5/31    2799.82    3000.00         2
11 2022/5/31    2307.36    3000.00         3
12 2022/5/31    2810.71    3000.00         2
13 2022/5/31    3032.91    3000.00         1

绘制了预测值

library(ggplot2)
ggplot(x, aes(x=date, y=pred_value, color=acc_level)) +
  geom_point(size=2, alpha=0.7, position=position_jitter(w=0.1, h=0)) +
  theme_bw()

我已经用代码 =“ https://i.sstatic.net/zyvmr.png” rel =“ nofollow noreferrer”>

除了我上面所做的事情之外,如果我希望用红线和红点绘制每个月的实际值,我该怎么办?谢谢。

参考:

如何添加4组以用平均段制作分类散点图?

For a sample dataframe df, pred_value and real_value respectively represent the monthly predicted values and actual values for a variable, and acc_level represents the accuracy level of the predicted values comparing with the actual values for the correspondent month, the smaller the values are, more accurate the predictions result:

df <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L), .Label = c("2022/3/31", "2022/4/30", 
"2022/5/31"), class = "factor"), pred_value = c(2721.8, 2721.8, 
2705.5, 2500, 2900.05, 2795.66, 2694.45, 2855.36, 2300, 2799.82, 
2307.36, 2810.71, 3032.91), real_value = c(2736.2, 2736.2, 2736.2, 
2736.2, 2736.2, 2759.98, 2759.98, 2759.98, 2759.98, 3000, 3000, 
3000, 3000), acc_level = c(1L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 
2L, 3L, 2L, 1L)), class = "data.frame", row.names = c(NA, -13L
))

Out:

        date pred_value real_value acc_level
1  2022/3/31    2721.80    2736.20         1
2  2022/3/31    2721.80    2736.20         1
3  2022/3/31    2705.50    2736.20         2
4  2022/3/31    2500.00    2736.20         3
5  2022/3/31    2900.05    2736.20         3
6  2022/4/30    2795.66    2759.98         1
7  2022/4/30    2694.45    2759.98         2
8  2022/4/30    2855.36    2759.98         2
9  2022/4/30    2300.00    2759.98         3
10 2022/5/31    2799.82    3000.00         2
11 2022/5/31    2307.36    3000.00         3
12 2022/5/31    2810.71    3000.00         2
13 2022/5/31    3032.91    3000.00         1

I've plotted the predicted values with code below:

library(ggplot2)
ggplot(x, aes(x=date, y=pred_value, color=acc_level)) +
  geom_point(size=2, alpha=0.7, position=position_jitter(w=0.1, h=0)) +
  theme_bw()

Out:

enter image description here

Beyond what I've done above, if I hope to plot the actual values for each month with red line and red points, how could I do that? Thanks.

Reference:

How to add 4 groups to make Categorical scatter plot with mean segments?

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

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

发布评论

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

评论(1

殤城〤 2025-02-12 20:15:26

我们可以使用其他层添加实际层。为了使行显示出来,我们需要指定这些点应该是同一系列的一部分。

GGPLOT默认情况下假设由于X轴是离散的,因此数据点不属于同一组的一部分。我们可以通过将 date 变量变成日期数据类型来解决此问题,例如 aes(x = as.date(date)...

library(ggplot2)
ggplot(df, aes(x=date, y=pred_value, color=as.factor(acc_level))) +
  geom_point(size=2, alpha=0.7, position=position_jitter(w=0.1, h=0)) +
  geom_point(aes(y = real_value), size=2, color = "red") + 
  geom_line(aes(y = real_value, group = 1), color = "red") +
  scale_color_manual(values = c("yellow", "magenta", "cyan"),
                     name = "Acc Level") +
  theme_bw()

We can add the actuals using additional layers. To make the line show up, we need to specify that the points should be part of the same series.

ggplot assumes by default that since the x axis is discrete that the data points are not part of the same group. We could alternatively deal with this by making the date variable into a date data type, like with aes(x=as.Date(date)...

library(ggplot2)
ggplot(df, aes(x=date, y=pred_value, color=as.factor(acc_level))) +
  geom_point(size=2, alpha=0.7, position=position_jitter(w=0.1, h=0)) +
  geom_point(aes(y = real_value), size=2, color = "red") + 
  geom_line(aes(y = real_value, group = 1), color = "red") +
  scale_color_manual(values = c("yellow", "magenta", "cyan"),
                     name = "Acc Level") +
  theme_bw()

enter image description here

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