Faceted dodged barplot in R
Dr. Vipul Patel
Assistant Professor - Business Analytics / Marketing Analytics Trainer - PowerBI, Microsoft Excel, Business Analytics (R and Python)
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.
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.
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.
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.
Resulting table will look like below:
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.
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.
This will produce following beautiful faceted dodged barplot.
Excellent....Isn't it beautiful?...Thank you...
SAP Finance Consultant @PwC || Ex-KPMG
4 年Excellent sir.. really helpful to learn..?