Plot in R with the percentage change between 2022 and 2023.

huangapple go评论110阅读模式
英文:

Plot in R with the percentage change beetwen 2022 and 2023

问题

I'd like a little legend above each column that shows the change % using this formula: '((monthvalue2023-monthvalue2022)/monthvalue2022)*100'. So, for October, it would be '((319.4 - 235.7)/235.7)*100', which is 61.7%.

英文:

I have a dataset with the column Date and a column Price, I would like a graph that shows the percentage change beetwen the months.

  1. Date Price
  2. 30/10/2022 129,60
  3. 31/10/2022 106,10
  4. 01/11/2022 87,40
  5. 02/11/2022 103,80
  6. 01/12/2022 60,00
  7. 02/12/2022 380,50
  8. 01/10/2023 243,40
  9. 02/10/2023 76,00
  10. 08/11/2023 318,30
  11. 09/12/2023 156,30
  12. 10/12/2023 49,00
  13. this is what I have done:
  14. # Estrazione del mese e dell'anno
  15. df$Month<- format(df$Data, "%m")
  16. df$Year <- format(df$Data, "%Y")
  17. # Calcolo del totale del prezzo per ciascun mese
  18. total<- aggregate(Price ~ Month + Year, df, sum)
  19. # Creazione del grafico a istogramma
  20. plot <- plot_ly(totali_mese, x = ~Month, y = ~Price, color = ~Year, type = "bar") %>%
  21. layout(
  22. barmode = "group",
  23. xaxis = list(title = "Month"),
  24. yaxis = list(title = "Price"),
  25. legend = list(title = "Year"),
  26. x = totali_mese$Mese[2:nrow(totale)],
  27. y = totali_mese$Prezzo[2:nrow(totale)] + 20
  28. )
  29. #
  30. plot

these codes show of course the graph that I wanted, but now I'd like a little legend above each colmun that show the change %

(I'd used this formula: '((monthvalue2023-monthvalue2022)/montvalue2022)*100').

so for october I'd have '((319,4 - 235,7)/235,7)*100' which is 61,7%

this is the graph (the values are different 'cause I used a different dataset) Plot in R with the percentage change between 2022 and 2023.

答案1

得分: 1

以下是您要翻译的内容:

  1. library(plotly)
  2. library(dplyr)
  3. library(tidyr)
  4. library(lubridate)
  5. df_wide <- df_ %>%
  6. group_by(Year = year(Date), Month = month(Date)) %>%
  7. summarise(Total = sum(Price), .groups = "drop") %>%
  8. pivot_wider(names_from = Year, values_from = Total) %>%
  9. mutate(Change_pct = ((`2023` - `2022`) / `2022`) %>% scales::label_percent()())
  10. df_wide
  11. #> # A tibble: 3 × 4
  12. #> Month `2022` `2023` Change_pct
  13. #> <dbl> <dbl> <dbl> <chr>
  14. #> 1 10 236. 319. 36%
  15. #> 2 11 191. 318. 66%
  16. #> 3 12 440. 205. -53%
  17. plot_ly(df_wide, x = ~ Month) %>%
  18. add_bars(y = ~ `2022`, name = "2022") %>%
  19. add_bars(y = ~ `2023`, name = "2023", text = ~ Change_pct , textposition = "outside") %>%
  20. layout(yaxis = list(title = "Price"))

示例数据:

  1. df_ <- structure(list(Date = structure(c(19295, 19296, 19297, 19298,
  2. 19327, 19328, 19631, 19632, 19669, 19700, 19701), class = "Date"),
  3. Price = c(129.6, 106.1, 87.4, 103.8, 60, 380.5, 243.4, 76,
  4. 318.3, 156.3, 49)), row.names = c(NA, -11L), class = c("tbl_df",
  5. "tbl", "data.frame"))

Created on 2023-05-25 with reprex v2.0.2

  1. <details>
  2. <summary>英文:</summary>
  3. With just 2 years I&#39;d go with wide format, column per year. Makes change calculation easier to follow and when adding 2 traces to Plotly, we can just add text labels to one of those:
  4. ``` r
  5. library(plotly)
  6. library(dplyr)
  7. library(tidyr)
  8. library(lubridate)
  9. df_wide &lt;- df_ %&gt;%
  10. group_by(Year = year(Date), Month = month(Date)) %&gt;%
  11. summarise(Total = sum(Price), .groups = &quot;drop&quot;) %&gt;%
  12. pivot_wider(names_from = Year, values_from = Total) %&gt;%
  13. mutate(Change_pct = ((`2023` - `2022`) / `2022`) %&gt;% scales::label_percent()())
  14. df_wide
  15. #&gt; # A tibble: 3 &#215; 4
  16. #&gt; Month `2022` `2023` Change_pct
  17. #&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;
  18. #&gt; 1 10 236. 319. 36%
  19. #&gt; 2 11 191. 318. 66%
  20. #&gt; 3 12 440. 205. -53%
  21. plot_ly(df_wide, x = ~ Month) %&gt;%
  22. add_bars(y = ~ `2022`, name = &quot;2022&quot;) %&gt;%
  23. add_bars(y = ~ `2023`, name = &quot;2023&quot;, text = ~ Change_pct , textposition = &quot;outside&quot;) %&gt;%
  24. layout(yaxis = list(title = &quot;Price&quot;))

Plot in R with the percentage change between 2022 and 2023.<!-- -->

Sample data:

  1. df_ &lt;- structure(list(Date = structure(c(19295, 19296, 19297, 19298,
  2. 19327, 19328, 19631, 19632, 19669, 19700, 19701), class = &quot;Date&quot;),
  3. Price = c(129.6, 106.1, 87.4, 103.8, 60, 380.5, 243.4, 76,
  4. 318.3, 156.3, 49)), row.names = c(NA, -11L), class = c(&quot;tbl_df&quot;,
  5. &quot;tbl&quot;, &quot;data.frame&quot;))

<sup>Created on 2023-05-25 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月25日 18:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定