Faceted dodged barplot in R

Faceted dodged barplot in R

In this article, I will try to explain how to create faceted dodged barplot in R. Dataset used can be downloaded from following website:

https://essentials.togaware.com/ports.xlsx

The spread sheet contains multiple tables. We will extract the various tables of data from the spreadsheet with specific cell ranges for visualization. 

Let’s load the spread sheet into R.

No alt text provided for this image

Now we are interested in the dollar value and of imports and exports through sea ports over a period of 11 years. The table is found to range over rows 71 to 93 and consist of the first four columns. We ignore the first row of column names. Lets extract the required data as “mydata” and give proper names to columns. 

No alt text provided for this image

The location is either all Australian ports or just the 17 that are of interest for the analysis. The dollar value of the export and import through either all Australian ports or the 17 largest ports is reported in millions of dollars.

Observing the dataset we can see that export and import are currently character datatypes. Actually, they should be numeric. Lets concert them to numeric.

No alt text provided for this image

Another observation is that every second value of the period column is missing. This is typical of spreadsheet data where the table has an implied value of the cell above. We can use a simple indexing trick to replicate the period appropriately.

No alt text provided for this image

Resulting table will look like below:

No alt text provided for this image





We will now continue processing the dataset to reshape it into a form that suits the plot that we wish to generate. This process is often referred to as reshaping the dataset.

No alt text provided for this image

The two coloums of export and import will be merged into one column called type. The resulting table will have four columns named period, location, type and value. Now the data is ready to generate faceted dodged barplot.

No alt text provided for this image

This will produce following beautiful faceted dodged barplot.

No alt text provided for this image

Excellent....Isn't it beautiful?...Thank you...

Rahul Jaiswal

SAP Finance Consultant @PwC || Ex-KPMG

4 年

Excellent sir.. really helpful to learn..?

回复

要查看或添加评论,请登录

Dr. Vipul Patel的更多文章