I'm automating my bar charts and i'm stuck when it comes to order of the items. In my data i'm showing color of sold items. I want it to show 3 best selling colors and others combined. I want the 3 best selling colors to be in order of most soled to the least. And other to be always last. In my mock data i would like to get the following charts.
My problem is that i know how to set the order with fct_relevel but how do i automate it that it always relevels it so that other is last and the colors are in the correct order? The colors and the order of colors changes from item to item. I need it to work for any item and color without having to rewrite anything in code.
DF<- read_xlsx("C:/Users/Desktop/TEST_R.xlsx")
DF <- DF %>%
filter(Item == "Cars")
DF$Color<- fct_relevel(DF$Color, "Other","Red","Blue","Green") #For Cars
DF$Color<- fct_relevel(DF$Color, "Other","Blue","Black","Red") #For Trucks
ggplot(DF ,aes(y=Color, x=Sales))+
geom_bar(stat = 'identity')+
labs(title=DF$Item)+
theme_minimal()
My sample data
Item Color Sales Cars Other 500 Cars Green 200 Cars Red 75 Cars Blue 100 Trucks Other 150 Trucks Black 50 Trucks Red 100 Trucks Blue 25
Thank you