R Tutorial: Plotting a single variable

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video tutorial teaches how to create interactive histograms and bar charts using Plotly in R, covering the distribution of wine types and phenols in the wine data set. It explains the fundamental parts of a Plotly graphic and how to customize the plots. By the end of this lesson, you will be able to create interactive plots to visualize univariate data.

Key Takeaways
  1. Load the necessary libraries (ggplot2, plotly, forcats)
  2. Calculate a frequency table using the count command
  3. Create a base layer using the plot_ly command
  4. Map variables to aesthetics using ~
  5. Add a trace to create a bar chart or histogram
  6. Use the reorder command to sort bars in descending order
  7. Adjust the binning scheme using the n_bins or x_bins arguments
💡 To effectively visualize univariate data, it is essential to choose the right type of plot (e.g., bar chart or histogram) and customize it to reveal interesting features of the data.

Related Reads

Up next
Best AI Tools for Social Media Marketing in 2026 (Beginner-Friendly AI Tools)
Pin Generator
Watch →