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

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

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.


Date         Price
30/10/2022	 129,60 € 
31/10/2022	 106,10 € 
01/11/2022	 87,40 € 
02/11/2022	 103,80 € 
01/12/2022	 60,00 € 
02/12/2022	 380,50 € 
01/10/2023	 243,40 € 
02/10/2023	 76,00 € 
08/11/2023	 318,30 € 
09/12/2023	 156,30 € 
10/12/2023	 49,00 € 

this is what I have done:

# Estrazione del mese e dell'anno
df$Month<- format(df$Data, "%m")
df$Year <- format(df$Data, "%Y")

# Calcolo del totale del prezzo per ciascun mese
total<- aggregate(Price ~ Month + Year, df, sum)

# Creazione del grafico a istogramma
plot <- plot_ly(totali_mese, x = ~Month, y = ~Price, color = ~Year, type = "bar") %>%
  layout(
    barmode = "group",
    xaxis = list(title = "Month"),
    yaxis = list(title = "Price"),
    legend = list(title = "Year"),
      x = totali_mese$Mese[2:nrow(totale)],
      y = totali_mese$Prezzo[2:nrow(totale)] + 20
    )

# 
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

以下是您要翻译的内容:

library(plotly)
library(dplyr)
library(tidyr)
library(lubridate)

df_wide <- df_ %>%
  group_by(Year = year(Date), Month = month(Date)) %>%
  summarise(Total = sum(Price), .groups = "drop") %>%
  pivot_wider(names_from = Year, values_from = Total) %>%
  mutate(Change_pct = ((`2023` - `2022`) / `2022`) %>% scales::label_percent()())
df_wide
#> # A tibble: 3 × 4
#>   Month `2022` `2023` Change_pct
#>   <dbl>  <dbl>  <dbl> <chr>     
#> 1    10   236.   319. 36%       
#> 2    11   191.   318. 66%       
#> 3    12   440.   205. -53%

plot_ly(df_wide, x = ~ Month) %>%
  add_bars(y = ~ `2022`, name = "2022") %>%
  add_bars(y = ~ `2023`, name = "2023", text = ~ Change_pct , textposition = "outside") %>%
  layout(yaxis = list(title = "Price"))

示例数据:

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

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


<details>
<summary>英文:</summary>

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:

``` r
library(plotly)
library(dplyr)
library(tidyr)
library(lubridate)

df_wide &lt;- df_ %&gt;% 
  group_by(Year = year(Date), Month = month(Date)) %&gt;% 
  summarise(Total = sum(Price), .groups = &quot;drop&quot;) %&gt;% 
  pivot_wider(names_from = Year, values_from = Total) %&gt;% 
  mutate(Change_pct = ((`2023` - `2022`) / `2022`) %&gt;% scales::label_percent()())
df_wide
#&gt; # A tibble: 3 &#215; 4
#&gt;   Month `2022` `2023` Change_pct
#&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;chr&gt;     
#&gt; 1    10   236.   319. 36%       
#&gt; 2    11   191.   318. 66%       
#&gt; 3    12   440.   205. -53%

plot_ly(df_wide, x = ~ Month) %&gt;% 
  add_bars(y = ~ `2022`, name = &quot;2022&quot;) %&gt;% 
  add_bars(y = ~ `2023`, name = &quot;2023&quot;, text = ~ Change_pct , textposition = &quot;outside&quot;) %&gt;% 
  layout(yaxis = list(title = &quot;Price&quot;))

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

Sample data:

df_ &lt;- structure(list(Date = structure(c(19295, 19296, 19297, 19298, 
19327, 19328, 19631, 19632, 19669, 19700, 19701), class = &quot;Date&quot;), 
    Price = c(129.6, 106.1, 87.4, 103.8, 60, 380.5, 243.4, 76, 
    318.3, 156.3, 49)), row.names = c(NA, -11L), class = c(&quot;tbl_df&quot;, 
&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:

确定