Virajk
Virajk

Reputation: 29

R - convert numeric to percentage and geom_col

Can someone help me with the following?

my data frame looks like below:

tibble::tribble(
  
  ~date,         ~ratio_type,   ~percentage,
  "30/06/2016",  "gp",          0.25,
  "30/06/2016",  "EBIT",        0.20,
  "30/06/2017",  "gp",          0.30,
  "30/06/2017",  "EBIT",        0.18,
)

It reads date as date, ratio_type as character, and percentage as numeric.

I have two problems:

  1. How to convert numeric value to percentage in R
  2. In my plot(geom_col) I mapped date in the x axis and percentage in the y axis. I need separate columns for each ratio_type for the year. However, in my column chart, I get only one column for each year and ratio_type's are stacked in one column. How do I get separate columns for each ratio type for each year?

Upvotes: 0

Views: 419

Answers (1)

Kra.P
Kra.P

Reputation: 15143

1.scale::label_percent() convert numeric value to percentage in R

2.position = position_dodge() will separate columns and scale_y_continuous(labels = scales::percent) will make y axis percentage.

df %>% mutate(date = as.Date(date, "%d/%m/%Y")) %>%
  ggplot(aes(x = date, y= percentage, group = ratio_type, fill = ratio_type)) +
  geom_col(position = position_dodge()) + scale_y_continuous(labels = scales::percent)

enter image description here

Upvotes: 1

Related Questions