R Tutorial: Plotting a single variable
Key Takeaways
This video tutorial covers creating histograms and bar charts using Plotly in R, exploring the distribution of wine types and phenols in the wine data set.
Full Transcript
in the previous lesson you learned how to convert your static ggplot2 plots in interactive plotly charts not all ggplot objects can be converted to plot Li 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 plot Li as first examples of univariate graphics we'll explore the distribution of the wine types and phenols using the wine data set from the previous lesson 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 plot li graphic first we have the data set here we calculate a frequency table giving the number of wines of each type using the count command we then pass this summarized data set to the plot underscore ly command which creates our base layer similar to the GG plot function the second piece is the mapping of the variables in the data set to aesthetics in the graph here we specified the mappings using Tilda's x equals tilde type y equals tilde n telling the plot which variable defines each aesthetic third we specified the plot type by adding a trace similar to how we added geometry in ggplot2 to create a bar chart we add the pipe operator percent greater than percent after the plot underscore ly base layer and specify ad underscore 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 FC t underscore reorder command found in the for cats 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 an FC T underscore reorder reorders the levels of type by the values in n to organize the levels in descending order we add the argument dot des c equals true 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 data set into the plot underscore ly command next we specify x equals tilde 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 whenever you create a histogram it's important to explore different thinning 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 and plot Lee the first is to change the number of bins displayed by adding the n bins X argument to the add underscore histogram command here we specify that 10 bins should be displayed the second way to change the binning is to specify the exact values for the bins here we specify X bins equals lists parentheses start equals 0.8 end equals 4 and size equals 0.25 resulting in bins of with 0.25 spanning from point 8 to 4 if you specify the exact values for the bins be sure to look at the summary of the variable first so that you choose a logical start and end value
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/interactive-data-visualization-with-plotly-in-r at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
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.
As the first examples of univariate graphics, we'll explore the distribution of the wine types and phenols using the wine dataset from the previous lesson.
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 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.
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
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 12 of 60
1
2
3
4
5
6
7
8
9
10
11
▶
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Data Literacy
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI