R Tutorial : Constructing rbokeh Layers (Part 1)

Learn to build interactive visualizations with rbokeh in R using the pipe operator to add layers. The tutorial covers two examples: plotting Rwanda's life expectancy from GapMinder data and creating a population-versus-time chart from the economics dataset, showing both data-frame and vector-based approaches.
Chapters
In the previous lesson you got an overview about R. Boket and in the exercises you saw examples of R Boke plots. In this lesson you will learn how R Boke plots are constructed and how to specify data and arguments. R Bokeh plots are constructed by initializing a figure. This is like an empty canvas you can lay out and then add layers too. To add layers you can use the Pipe operator. If you previously used ggplot2, you will notice that while the pipe operator is used to add layers in rboquet, the plus operator is used in ggplot2. For example, let's say you want to plot life expectancy versus time in one country, Rwanda using the GapMinder data set. First you need to extract the entries corresponding to Rwanda using the filter function with the condition country Rwanda. To construct a plot you can use figure followed by the Pipe operator. Then you can add a lines layer using the Lylines function. In the Lylines layer you should pass year to the X argument, life expectancy to the Y argument, and the new data frame data Rwanda to the data argument. The resulting plot shows a line with a significant drop in life expectancy around 1990, the year that marked the start of the war in Rwanda that lasted for almost four years. So far we've seen one way of specifying data in R Bokeh layers where you pass a data frame to the data argument and the column names from this data frame to the X and Y arguments. The other way is to pass vectors to the X and Y arguments. This will produce the same output except for the axis labels. You will see later how to change the axis labels and you will learn more about the figure attributes in the next chapter. In the course we will stick to the first way of specifying data by passing the data frame and the columns names. Now let's take a new data set, the Economics DataSet from the ggplot2 package. The data set includes stats about the US economics, including the personal consumption expenditures in billions of dollars, the number of unemployed unemployed and population pop in thousands. If you want to create a line plot with population versus time, you can simply specify the X and data arguments in the ly lines layer. Notice that you can assign the resulting figure to plot pop, then print it by writing Plot pop. This is because our Bokeh figures are objects that you can save, retrieve and modify later. Now it's time to practice. In the next set of exercises you will practice more using the economics data.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







