Jaskeil
Jaskeil

Reputation: 1232

How to format labels of a bar chart in dollar format in R?

Looking to add a dollar sign on the labels on my x axis and the numbers in the bar chart. Below is my code and the chart.

YTD_bar <- 
      ggplot(TYSales_LYSales, aes(x=as.character(FSCL_YR), y=SALES)) + geom_bar(stat="identity", fill="orange", color="grey40") + theme_bw() + coord_flip() + 
      geom_text(aes(x= as.character(FSCL_YR), y=0.01, label= SALES),
                hjust=-0.8, vjust=-1, size=3, 
                colour="black", fontface="bold",
                angle=360) + labs(title="D27 2020 YTD Sales v 2019 YTD Sales", x="Fiscal Year",y="Sales") + theme(plot.title=element_text(hjust=0.5))
    YTD_bar

enter image description here

enter image description here

Upvotes: 3

Views: 2988

Answers (3)

Dave2e
Dave2e

Reputation: 24139

The scales package (installed with ggplot2) has the handy dollar and label_dollar() functions for converting the decimal values into currency.

See help to understand the may options available to adjust the formatting.

library(ggplot2)
library(scales)

YTD_bar <- 
   ggplot(TYSales_LYSales, aes(x=as.character(FSCL_YR), y=SALES)) + 
   geom_bar(stat="identity", fill="orange", color="grey40") + 
   theme_bw() + coord_flip() + 
   geom_text(aes(x= as.character(FSCL_YR), y=0.01, label= dollar(SALES)),
             hjust=-0.8, vjust=-1, size=3, colour="black", fontface="bold", angle=360) + 
   labs(title="D27 2020 YTD Sales v 2019 YTD Sales", x="Fiscal Year",y="Sales") + 
   theme(plot.title=element_text(hjust=0.5)) +
   scale_y_continuous(labels = label_dollar())
YTD_bar

enter image description here

Upvotes: 4

Ben Toh
Ben Toh

Reputation: 782

You can specify the label yourself and add a dollar sign in front by using paste0() function

df <- data.frame(yr = 2019:2020, sales = c(1234, 5678))
df$text <- paste0("$", df$sales)

ggplot(df, aes(x=as.character(yr), y=sales)) + 
  geom_col(fill="orange", color="grey40") + 
  theme_bw() + 
  coord_flip() + 
  geom_text(aes(x = as.character(yr), y=0.01, label= text),
            hjust=-0.8, vjust=-1, size=3, 
            colour="black", fontface="bold",
            angle=360) +
  scale_y_continuous(breaks = 0:3 * 2000,
                     labels = paste0("$", 0:3 * 2000))

Upvotes: 1

duckmayr
duckmayr

Reputation: 16940

You can do this with liberal application of sprintf("$%0.2f", ...). The %0.2f part tells is to format as floating point numbers, but with two decimal places. You need to do this in two places: (1) within geom_text(), and (2) as part of a call to scale_y_continuous():

YTD_bar <- ggplot(TYSales_LYSales, aes(x=as.character(FSCL_YR), y=SALES)) +
    geom_bar(stat="identity", fill="orange", color="grey40") +
    theme_bw() +
    coord_flip() + 
    geom_text(aes(x = as.character(FSCL_YR), y=0.01,
                  label= sprintf("$%0.2f", SALES)),
              hjust=-0.8, vjust=-1, size=3, 
              colour="black", fontface="bold",
              angle=360) +
    labs(title="D27 2020 YTD Sales v 2019 YTD Sales",
         x="Fiscal Year", y="Sales") +
    theme(plot.title=element_text(hjust=0.5)) +
    scale_y_continuous(labels = function(breaks) sprintf("$%0.2f", breaks))
YTD_bar

enter image description here

Data

TYSales_LYSales <- data.frame(
    FSCL_YR = 2019:2020,
    SALES   = c(61851186, 5511072)
)

Upvotes: 1

Related Questions