Home > Software design >  R - Add Label and Remove a Bar from Bar Chart
R - Add Label and Remove a Bar from Bar Chart

Time:12-05

I have a data frame results_df with models and their respective performance metrics.

There are 9 models shown on the bar plot below. I would like to remove the AR(1) model (First bar) and add labels over each bar to count the y-axis (MAPE) values. Is there a way to do this?

results_df: dput(results_df)

structure(list(Model = c("Naive (baseline)", "Naive (baseline)", 
"Holt-Winter's Smoothing", "Holt-Winter's Smoothing", "Exponential Smoothing (AAN)", 
"Exponential Smoothing (AAN)", "Exponential Smoothing (AAN)", 
"Exponential Smoothing (AAN)", "Exponential Smoothing (MMN)", 
"Exponential Smoothing (MMN)", "Exponential Smoothing (MMdN)", 
"Exponential Smoothing (MMdN)", "Simple Regression", "Simple Regression", 
"AR(1)", "AR(1)", "ARIMA", "ARIMA", "Neural Network Autoregression", 
"Neural Network Autoregression", "Neural Network Autoregression", 
"Neural Network Autoregression", "Neural Network Autoregression", 
"Neural Network Autoregression", "Neural Network Autoregression", 
"Neural Network Autoregression", "Neural Network Autoregression", 
"Neural Network Autoregression"), Set = c("Train", "Test", "Train", 
"Test", "Train", "Test", "Train", "Test", "Train", "Test", "Train", 
"Test", "Train", "Test", "Train", "Test", "Train", "Test", "Train", 
"Test", "Train", "Test", "Train", "Test", "Train", "Test", "Train", 
"Test"), ME = c(783.614908859554, 261.373668023744, 14.3109110454195, 
195.100466156422, 26.3876287540668, 463.951556368907, 18.0130216685751, 
359.483123328568, -22.1643363307684, -339.196046699131, 12.6912377584583, 
518.09851679099, 0.0000000000000174739705362564, -529.41545982092, 
-1.22546154090165, 3925.73996146196, 8.85383136321011, 124.533565617977, 
0.284309224688646, -6.72677362104004, 0.284309224688646, -6.72677362104004, 
0.284309224688646, -6.72677362104004, 0.284309224688646, -6.72677362104004, 
0.284309224688646, -6.72677362104004), RMSE = c(1217.13789794065, 
751.642354127382, 500.820891022085, 477.824050637917, 519.473902002702, 
642.857862958026, 519.161630651347, 557.229990542789, 523.067637426936, 
589.028868073331, 528.35952997731, 686.44615027821, 524.569099190936, 
735.240889009488, 454.850494992402, 3949.12093581915, 512.863753849202, 
533.856079481684, 135.490259727469, 485.401392424817, 135.490259727469, 
485.401392424817, 135.490259727469, 485.401392424817, 135.490259727469, 
485.401392424817, 135.490259727469, 485.401392424817), MAE = c(996.051963196766, 
506.736876037775, 260.159300687626, 353.225967700696, 294.653723455236, 
540.046166565811, 295.649955005283, 444.757424142394, 309.824947101753, 
423.457221068304, 306.092832486747, 589.143961366613, 392.62157043408, 
583.197541495569, 287.909371174162, 3925.73996146196, 248.525867346098, 
371.164249425045, 84.7526369385231, 293.994074536976, 84.7526369385231, 
293.994074536976, 84.7526369385231, 293.994074536976, 84.7526369385231, 
293.994074536976, 84.7526369385231, 293.994074536976), MPE = c(17.1491069887359, 
4.32046155430935, -6.74519182268278, 2.87120046577417, -6.82283325669752, 
9.90612395387752, -7.20214793423834, 7.23484722049863, -8.92406901313896, 
-10.618241704038, -7.35920566787723, 11.316397549041, -8.15817902292851, 
-16.2120961149314, 278.150530756537, 100.940759473326, -4.78085408038733, 
0.903132637273637, -1.0450388130686, -3.16256781033905, -1.0450388130686, 
-3.16256781033905, -1.0450388130686, -3.16256781033905, -1.0450388130686, 
-3.16256781033905, -1.0450388130686, -3.16256781033905), MAPE = c(37.6646414975729, 
14.2674058209844, 13.4253935410948, 10.2678719351011, 17.2343529926111, 
14.5662838896106, 17.3299734983734, 12.2848516454307, 18.1093841365566, 
12.5514842599768, 17.8614665736195, 15.7476258421098, 21.2960874782265, 
17.3861366179269, 328.041997124712, 100.940759473326, 12.5829038948982, 
10.9566576520148, 3.39520095739983, 9.9164317537263, 3.39520095739983, 
9.9164317537263, 3.39520095739983, 9.9164317537263, 3.39520095739983, 
9.9164317537263, 3.39520095739983, 9.9164317537263), MASE = c(1, 
0.508745421686069, NaN, NaN, 0.295821638169924, 0.542186739768643, 
0.2968218184686, 0.446520302730967, 0.311052995776836, 0.425135672349106, 
0.307306088232949, 0.591479142790696, 0.39417779889112, 0.585509153180959, 
0.374562870610186, 5.10728922520835, 0.2495109457427, 0.372635427808221, 
0.0850885697434046, 0.295159374610759, 0.0850885697434046, 0.295159374610759, 
0.0850885697434046, 0.295159374610759, 0.0850885697434046, 0.295159374610759, 
0.0850885697434046, 0.295159374610759), ACF1 = c(0.477101737419842, 
-0.236641331303488, -0.0688752655501213, 0.0486772478154593, 
0.121398483630841, 0.085203686984427, 0.124889761137534, 0.00218743715991606, 
0.191776077581811, 0.186812955247135, 0.209332741368281, 0.105740160073441, 
0.470691853904666, 0.0555995310408417, -0.00100961596465889, 
0.0152620209423001, -0.0743813163863567, 0.0591555324997216, 
0.0603986214142024, 0.00539281074890409, 0.0603986214142024, 
0.00539281074890409, 0.0603986214142024, 0.00539281074890409, 
0.0603986214142024, 0.00539281074890409, 0.0603986214142024, 
0.00539281074890409), Theil.s.U = c(NA, 0.679390982324587, NA, 
0.483244519716164, NA, 0.658305710036264, NA, 0.572169692732178, 
NA, 0.554481369845331, NA, 0.704434769895274, NA, 0.724366674060867, 
NA, 4.02609591299822, NA, 0.512019993546249, NA, 0.403328477993201, 
NA, 0.403328477993201, NA, 0.403328477993201, NA, 0.403328477993201, 
NA, 0.403328477993201)), row.names = c("Training set", "Test set", 
"Training set1", "Test set1", "Training set2", "Test set2", "Training set3", 
"Test set3", "Training set4", "Test set4", "Training set5", "Test set5", 
"Training set6", "Test set6", "Training set7", "Test set7", "Training set8", 
"Test set8", "Training set9", "Test set9", "Training set10", 
"Test set10", "Training set11", "Test set11", "Training set12", 
"Test set12", "Training set13", "Test set13"), class = "data.frame")

Code:

results_df_reshape = reshape2::melt(results_df, c("MAPE", "Model"))

ggplot(results_df_reshape)  
  geom_bar(aes(x = Model, y = MAPE, fill = Model),
           stat = "identity",
           position = "dodge")  
  labs(title = "MAPE of All Models",
       x = "Model",
       y = "MAPE",
       bty = "l")

Bar plot: Model MAPE

CodePudding user response:

library(dplyr)
library(ggplot2)

results_df_reshape <-
  reshape2::melt(results_df, c("MAPE", "Model")) %>% 
  #Removal of AR(1)
  filter(Model != "AR(1)")

results_df_reshape %>%
  ggplot()  
  geom_bar(aes(x = Model, y = MAPE, fill = Model),
           stat = "identity",
           position = "dodge")  
  #Label for MAPE
  geom_text(
    data = results_df_reshape %>% 
      group_by(Model) %>% 
      summarise(MAPE = max(MAPE),label_mape = round(sum(MAPE,na.rm= TRUE),2)),
    aes(x = Model, y = MAPE,label = label_mape),
    nudge_y = 1) 
  labs(title = "MAPE of All Models",
       x = "Model",
       y = "MAPE",
       bty = "l")

enter image description here

CodePudding user response:

I have used the sum:

library(tidyverse)

df %>% 
  pivot_longer(-c(Model, MAPE, Set)) %>% 
  filter(Model != "AR(1)") %>% 
  group_by(Model) %>% 
  summarise(Mape_sum= sum(MAPE, na.rm = TRUE)) %>% 
  ggplot(aes(x = Model, y = Mape_sum, fill = Model)) 
  geom_col() 
  geom_text(aes(label=round(Mape_sum, 1)), size=3.5, vjust=-1) 
  labs(title = "MAPE of All Models",  x = "Model", y = "MAPE", bty = "l")

enter image description here

  • Related