如何在GGPLOT中绘制季度和年度的价值?

发布于 2025-02-07 01:51:15 字数 1126 浏览 0 评论 0原文

原始数据

structure(list(attainment_target = c(7.5, 15), quarter_2022 = c("Q1", 
"Q2"), total_attainment = c(2, 4), percent_attainment = c(0.2666, 
0.2666)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", 
"data.frame"))


Quarter    | Target  | Attainment 

2022-01-01    7.5          2
2022-04-01    15           4

方案

我想以四分之一为X轴和达到目标的GGPLOT(GEOM_COL或GEOM_BAR),作为Y轴,将目标作为水平破折号线,显示出我离该值离该值的距离。

但是,我在同一地块中绘制YTD(总成绩)的绘制(总成绩)很难。这是我如何使用dplyr创建新字段的一个示例,该字段显示了计算出的YTD值:

所需的输出

Quarter    | Target  | Attainment  | YTD. | % Attainment 

2022-01-01    7.5          2         2       27
2022-04-01    15           4         6       40

,这是通过R中的GGPLOT绘制此内容的最佳方法?这是我当前的方法,但在整合以上所有方面都很难:

df1 <- df %>%
    mutate(YTD_TOTAL = sum(total_attainment)) %>%
    mutate(YTD_PERCENT_ATTAINMENT = sum(total_attainment) / max(attainment_target))


ggplot(data = df1, aes(fill=quarter_2022, x=attainment_target, y=total_attainment, color = quarter_2022, palette = "Paired",
label = TRUE,
position = position_dodge(0.9)))

Raw data

structure(list(attainment_target = c(7.5, 15), quarter_2022 = c("Q1", 
"Q2"), total_attainment = c(2, 4), percent_attainment = c(0.2666, 
0.2666)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", 
"data.frame"))


Quarter    | Target  | Attainment 

2022-01-01    7.5          2
2022-04-01    15           4

Scenario

I would like to plot a ggplot (geom_col or geom_bar) with Quarter as x-axis and Attainment as y-axis with Target as a horizontal dash line that shows how far off I am from that value.

However, I am having trouble plotting YTD (Total attainment given # of quarters) in the same plot. Here is an example of how I used dplyr to create new field that shows calculated YTD value:

Desired output

Quarter    | Target  | Attainment  | YTD. | % Attainment 

2022-01-01    7.5          2         2       27
2022-04-01    15           4         6       40

Which is the best way to plot this via ggplot in R? Here is my current approach but having trouble incorporating all the above:

df1 <- df %>%
    mutate(YTD_TOTAL = sum(total_attainment)) %>%
    mutate(YTD_PERCENT_ATTAINMENT = sum(total_attainment) / max(attainment_target))


ggplot(data = df1, aes(fill=quarter_2022, x=attainment_target, y=total_attainment, color = quarter_2022, palette = "Paired",
label = TRUE,
position = position_dodge(0.9)))

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

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

发布评论

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

评论(1

可是我不能没有你 2025-02-14 01:51:15

不确定您有什么想法,但是这里可能要使用的一些部分:

df %>%
  mutate(YTD_TOTAL = cumsum(total_attainment)) %>%
  mutate(YTD_PERCENT_ATTAINMENT = YTD_TOTAL/ attainment_target) %>%
  
  ggplot(aes(quarter_2022, total_attainment)) +
  geom_col(aes(y = YTD_TOTAL), fill = NA, color = "gray20") +
  geom_text(aes(y = YTD_TOTAL, label = scales::percent(YTD_PERCENT_ATTAINMENT)),
            vjust = -0.5) +
  geom_col(fill = "gray70", color = "gray20") +
  geom_text(aes(label = total_attainment),
            position = position_stack(vjust = 0.5)) +
  geom_segment(aes(x = as.numeric(as.factor(quarter_2022)) - 0.4, 
                   xend = as.numeric(as.factor(quarter_2022)) + 0.4,
                   y = attainment_target, yend = attainment_target),
               linetype = "dashed")

“在此处输入图像说明”

Not sure exactly what you have in mind but here are some of the pieces you might want to use:

df %>%
  mutate(YTD_TOTAL = cumsum(total_attainment)) %>%
  mutate(YTD_PERCENT_ATTAINMENT = YTD_TOTAL/ attainment_target) %>%
  
  ggplot(aes(quarter_2022, total_attainment)) +
  geom_col(aes(y = YTD_TOTAL), fill = NA, color = "gray20") +
  geom_text(aes(y = YTD_TOTAL, label = scales::percent(YTD_PERCENT_ATTAINMENT)),
            vjust = -0.5) +
  geom_col(fill = "gray70", color = "gray20") +
  geom_text(aes(label = total_attainment),
            position = position_stack(vjust = 0.5)) +
  geom_segment(aes(x = as.numeric(as.factor(quarter_2022)) - 0.4, 
                   xend = as.numeric(as.factor(quarter_2022)) + 0.4,
                   y = attainment_target, yend = attainment_target),
               linetype = "dashed")

enter image description here

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