Here is the YouTube Video link for this tutorial.

Video link https://youtu.be/Yes-GVKHFgY

Audience

For beginners or experienced R users wanting to learn various commands of dplyr and ggplot.

Packages used

library(ggplot2)
library(dplyr)
library(gapminder)

Sample dataset.

We are using the gapminder package to create our sample data.

df <- gapminder::gapminder

df.2007 <- df%>%
  dplyr::filter(year == 2007)%>%
  dplyr::arrange(-pop)%>%
  dplyr::top_n(10,wt = pop)

First Chart

You would notice that on the y axis the population of the countries is shown in scientific notation.

pl <- ggplot(data = df.2007, aes(x = country, y = pop))
pl <- pl + geom_bar(stat ="identity")
pl <- pl + theme_bw()
pl

Fix the numbers

Option(scipen = 999) is a good way of handing it. Always include this line at the top of your script, if you don’t want to see the scientific notations in your numbers and charts.

options(scipen = 999)
library(scales)
pl <- ggplot(data = df.2007, aes(x = country, y = pop))
pl <- pl + geom_bar(stat ="identity")
pl <- pl + theme_bw()
pl

Formatting the numbers with commas

options(scipen = 999)
library(scales)
pl <- ggplot(data = df.2007, aes(x = country, y = pop))
pl <- pl + geom_bar(stat ="identity")
pl <- pl + scale_y_continuous(labels = scales::comma)
pl <- pl + theme_bw()
pl

Show the numbers as Millions and Billions

scales::label_number_si() option is very useful as it will transform your labels to the correct format based on your numbers.

options(scipen = 999)
library(scales)
pl <- ggplot(data = df.2007, aes(x = country, y = pop))
pl <- pl + geom_bar(stat ="identity")
#pl <- pl + scale_y_continuous(labels = scales::comma)
pl <- pl + scale_y_continuous(labels = scales::label_number_si())
pl <- pl + theme_bw()
pl

Watch our complete tutorial on all aspects of DPLYR.

https://www.youtube.com/playlist?list=PLkHcMTpvAaXVJzyRSytUn3nSK92TJphxR