Univariate graphics
1. Plotting a single variable
In the previous lesson, you learned how to convert your static ggplot2 plots into interactive plotly charts. Not all ggplot objects can be converted to plotly objects, and sometimes you want more control over how your graphics are rendered. In this lesson, we'll explore how to create histograms and bar charts using plotly.2. Exploring the wine data
As first examples of univariate graphics, we'll explore the distribution of the wine types and phenols using the wine dataset from the previous lesson.3. Bar charts with plotly
To explore the distribution of wine type, a categorical variable, we use a bar chart displaying the number of wines of each type. There are three fundamental parts to a plotly graphic: First, we have the dataset. Here we calculate a frequency table giving the number of wines of each type using the count() command. We then pass this summarized dataset to the plot underscore ly command, which creates our base layer, similar to the ggplot() function. The second piece is the mapping of the variables in the dataset to aesthetics in the graph. Here, we specify the mappings using tildes, x = ~Type, y = ~n, telling the plot which variable defines each aesthetic. Third, we specify the plot type by adding a trace, similar to how we add a geometry in ggplot2. To create a bar chart we add the pipe operator, %>%, after the plot underscore ly base layer and specify add underscore bars.4. Reordering the bars
With only three wine types our bar chart was easy enough to read; however, with more categories bar charts can become difficult to read unless the bars are sorted. For example, we may wish to rearrange the bars in descending order. To do this we use the fct underscore reorder command found in the forcats package. To sort the bars in descending order we add a single line of code to our data-plot pipeline: Mutate creates a new variable, Type, and fct underscore reorder reorders the levels of Type by the values in n. To organize the levels in descending order, we add the argument dot-desc = TRUE.5. Histograms with plotly
To explore the distribution of phenols, a numeric variable, we use a histogram displaying the number of wines with phenols falling into equal-width bins. We again need to specify three parts: First, we pipe the wine dataset into the plot underscore ly command. Next, we specify x = ~Phenols, indicating that Phenols should be plotted on the x-axis. Finally, we add the histogram trace using the add underscore histogram command. Notice that we do not need to specify a variable for the y-axis here since plotly calculates the frequency for each bin in the background.6. Adjusting the number of bins
Whenever you create a histogram, it's important to explore different binning schemes, since bins that are too wide may mask interesting features of the data, and bins that are too small provide little insight. There are two ways to adjust the binning scheme in plotly. The first is to change the number of bins displayed by adding the nbinsx argument to the add_histogram() command. Here we specify that 10 bins should be displayed.7. Adjusting the bin width
The second way to change the binning is to specify the exact values for the bins. Here, we specify xbins equals list parenthesis start = point-8 , end = 4, and size = point-25, resulting in bins of width 0-point-25 spanning from 0-point-8 to 4. If you specify the exact values for the bins, be sure to look at a summary of the variable first so that you choose a logical start and end value.8. Let's practice!
Let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.