R Tutorial : Constructing rbokeh Layers (Part 1)

preview_player
Показать описание

---

In the previous lesson, you got an overview about rbokeh and in the exercises you saw examples of rbokeh plots. In this lesson you will learn how rbokeh plots are constructed and how to specify data and arguments.

rbokeh plots are constructed by initializing a figure(). This is like an empty canvas you can lay out and then add layers to.

To add layers, you can use the pipe operator.

If you previously used ggplot2, You will notice that while the the pipe operator is used to add layers in rbokeh, 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 dataset.

First, you need to extract the entries corresponding to rwanda using the filter function with the condition country == Rwanda

To construct the plot, you can use figure(), followed by the pipe operator,

then you can add a lines layer using the ly_lines() function

In the ly_lines layer, you should pass

- year to the x argument, lifeExp to the y argument
- and the new dataframe 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.

This will produce the same output except for the axes labels. You will see later how to change the axes 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 specifiying data by passing the dataframe and the columns names.

Now Let's take a new dataset, the economins dataset from the ggplot2 package. The dataset includes stats about the US econmoics including the personal consumption expenditures(pce) in billions of dollars, the number of unemployed (unemploy) and population (pop) in thousands .

If you want to create a line plot with population versus time, you can simply specify the x, y 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 rbokeh figures are objects that you can save, retrive and modify later.

Now it is time to practice. In the next set of exercises, you will practice more using the economics dataset.

#DataCamp #RTutorial #InteractiveDataVisualizationwithrbokeh
Рекомендации по теме