Code available to download.
A tutorial for beginners on how to start r coding scripts, installing R packages, and interactive chart visualization examples, Code available to download. Begin all your R scripts with this snippet! Automatically installing and load library packages using pacman + simple walkthrough example for an interactive bar chart for beginners.
This example is done in a R markdown format using RStudio. Files and code available to download. Subscribe to learn more about this type of coding and more!
In this tutorial video, you learn how to
1- begin your r codes with automatically loading and installing your required library packages
2- visualize a data example with interactive bar charts
- order the bar char in decreasing or increasing
- visualize multiple bar interactive charts
Start all your R code with automatically install if needed and load your required package libraries
Step1: use pacman library as shown in the first block to install and load any list of library packages you use. you can copy and use this type of code in the beginning of all your future R scripts.
Start your block with the highlighted code to clarify this is your initial setup and no data analysis is part of this block yet
In this example, my library packages are: tidyverse, dplyr (for routine R operations) and plotly (for an interactive visualization in next steps).
Now we are done for the automatics installation of packages. Rest of code are for a simple yet elegant and handy interactive visualization.
Interactive yet simple bar chart visualization – plotly in R
Step2: Let’s use an example of built-in datasets in R for practice.
For simplicity, I save it as a dataframe named df, and since there are some rownames, I add them as a new column.
Step3: Form the visualization question. Let’s look at the data table for clarification briefly.
This is a dataset on some car models and and their mechanical properties. You may wonder how they compare in terms of Miles/(US) gallon (mpg variable) and number of cylander (cyl variable).
We perform an interactive bar chart to visualize these two variables in more clear and interactive way next.
Step4: use this piece of code for a bar chart visualization in plotly on car categories and the mpg variable. You need to update the name for the bar chart and the name for the chart title.
Run and see the interactive result by hovering your mouse!
You may wonder, there is one problem with it. It is hard to grasp which cars are the top or bottom in mileage usage here.
So let’s order the chart by mpg variable in Step 5! You need to add the last lines of highlighted code for this step.
Order axis categories in bar chart – interactive plotly chart in R
You can add titles for x and y axis if you want here as well.
Voila! The result look much neater!
Multiple interactive bar charts – Plotly in R
Now, in last step, we see how to add multiple variables as bar charts in one interactive chart. using both cyl and mpg variables in Step 6.
You need to add the last line of code starting with “add_trace”… for this part, using number of cylanders as the second variables to visualize here.
Voila! Now you really see the value of interactiveness in this chart. You can toggle between the two variables by clicking on the legend!
Related links:
- code script template solution for this video available in this GitHub repo: https://github.com/winswithdata/Start_R
- Subscribe to learn move about R, data science, economics, Python and other tech solutions!
Pingback: Start all your R code with automatically install if needed and load your required package libraries - Wins With Data