0

I have two problems,

  1. I want my legends in the order of highest to lowest value or lowest to the highest value. But it shows up in the alphabetic order (EBIT, EBITDA, Gross Profit, Net Profit before Tax, Sales). However, I would like to have them in the order of Sales, Gross Profit, EBITDA, EBIT, Net Profit before Tax)
  2. In my x-axis I have columns for five years from 2017 to 2021. I would like to have all five years as labels instead of three years automatically showing up (2018, 2020, 2022)

I have done following in R Studio,

install.packages("tidyverse")
library(tidyverse)
install.packages("lubridate")
library(lubridate)
library(dplyr)
install.packages("scales")
library(scales)
library(ggplot2)
five_years_growth <- read.csv("five_years_growth.csv")
five_years_growth$year <- dmy(five_years_growth$year)
five_years_growth$amount <- as.numeric(five_years_growth$amount)
five_years_growth$category <- factor(five_years_growth$category)
ggplot(data = five_years_growth) +
  geom_col(mapping = aes(x = year, y = amount, group = reorder(category, -amount), fill = category), position = position_dodge()) +
  scale_y_continuous(name = "Amount $", label = comma) +
  labs(title = "Key Performance : 2017 to 2021", x = "Financial Year")

I would highly appreciate your help on the above

structure(list(year = structure(c(17347, 17712, 18077, 18443, 
18808, 17347, 17712, 18077, 18443, 18808, 17347, 17712, 18077, 
18443, 18808, 17347, 17712, 18077, 18443, 18808, 17347, 17712, 
18077, 18443, 18808), class = "Date"), category = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("Sales", "Gross Profit", 
"EBITDA", "EBIT", "Net Profit before Tax"), class = "factor"), 
    amount = c(1e+05, 2e+05, 3e+05, 4e+05, 5e+05, 50000, 1e+05, 
    150000, 2e+05, 250000, 40000, 80000, 130000, 150000, 175000, 
    30000, 65000, 1e+05, 120000, 150000, 25000, 50000, 72000, 
    1e+05, 125000)), row.names = c(NA, -25L), class = "data.frame")
Virajk
  • 31
  • 2
  • 2
    Welcome to SO! To help us to help would you mind making your issue reproducible by sharing a sample of your data or provide some fake data? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). To share your data type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Sep 03 '21 at 06:45
  • ... for your second issue you could try with `+ scale_x_continuous(breaks = 2017:2021)`. – stefan Sep 03 '21 at 06:49
  • ... for your first issue you could try with setting the levels of the factor in your desired order, i.e. ´five_years_growth$category <- factor(five_years_growth$category, levels = c("Sales", ....))` – stefan Sep 03 '21 at 06:50
  • Great. your solution to my first problem really worked well. Thank you. However, the solution for the second issue does not work. I have pasted the structure as you have noted. Hope it might help you to suggest a solution to the my second issue. – Virajk Sep 03 '21 at 14:09
  • As your year variable is a `Date` try with `scale_x_date(date_breaks = "1 year", date_labels = "%Y")`. One of the reasons why people on SO insist on getting the data as `dput()`. – stefan Sep 03 '21 at 15:40

1 Answers1

0

Code

df %>% 
  #Reorder category by amount
  mutate(category = fct_reorder(category,amount,.desc = TRUE)) %>% 
  ggplot() +
  geom_col(aes(x = year, y = amount, fill = category),
           position = position_dodge()) +
  #Setting x axis breaks to be 1 year, showing only the four digits of the year, e.g, 2000
  scale_x_date(date_breaks = "1 year",date_labels = "%Y")

enter image description here

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32