0

Can I have some help on how to insert a ranking of top 5 and bottom 5 Subvertical per Vertical? Thank you

vertical is the element and subvertical is the subelement.

Should produce an additional column after total "total" for the ranking of AHT's. Like this:

enter image description here

Data Processing for Subvertical
data_process.subvertical <- data_process %>% select(DATE, Vertical, Subvertical, AHT, total) %>%
  dplyr::group_by(DATE, Vertical, Subvertical) %>%
  dplyr::summarise(AHT = mean(AHT, na.rm = T), total = sum(total)) %>% 
  dplyr::ungroup() 

View(test_data)
Subvertical AHT
v <- ("Adult")  ##change vertical here
subvert.aht <- function(Vertical) {  
  if (Vertical == v) {
    filter1 <- data_process.subvertical %>%
      dplyr::filter(Vertical == v)

line_chart_cs <- ggplot2::ggplot(filter1, aes(x = DATE, y = AHT, colour = Subvertical)) +
  ggplot2::geom_line() +
  scale_x_date(breaks = filter1$DATE) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"),legend.position = "bottom" ) +
  ggtitle(paste("Monthly AHT -", v, "by Subvertical"))

    line_chart_cs
  }
  else {
    print("TEST.")
  }
}

subvert.aht(v)
V.Germano
  • 1
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 08 '21 at 22:58
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 09 '21 at 00:17
  • `data_process.subvertical %>% group_by(Vertical) %>% arrange(- AHT) %>% slice(1:5)` will give you the 5 subverticals with the highest AHT for each vertical – danlooo Sep 09 '21 at 09:28
  • thanks @danlooo. I'm just thinking where should I put this code? Would it be inside the "Subvertical AHT" part under the dplyr::filter(Vertical == v)? or in the data process itself? – V.Germano Sep 09 '21 at 13:22

0 Answers0