• Labs icon Lab
  • Data
Labs

Visualize Data with ggplot2 in R

n this lab, you will utilize your R skills to transform tabular data into insightful bivariate and multivariate visualizations. You will create bar charts, line charts, histograms, and scatter plots using a medical patient dataset. Additionally, you will enhance these charts by applying R techniques to improve clarity and readability.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 45m
Published
Clock icon Apr 09, 2025

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Introduction

    Visualize Data with ggplot2 in R

    Hello learners!

    In this lab, you will utilize your R skills to transform tabular data into insightful visualization. This lab will take you through guided steps on to write ggplot2 functions and customize them to your own needs.

    You will use a medical patients dataset 🩺🥼💉to achieve your lab objectives.


    Key Concepts

    1. Basic Plot Creation

      • Bar Chart
      • Line Chart
      • Histogram
    2. Data Manipulation

      • Age Calculation
    3. Chart Customization

      • Custom Colors
      • Labels and Titles
      • Scale Adjustment
    4. Multi-variable Plots

      • Facet Grids
      • Scatter Plot
      • Trend Line
      • Multiple Variables
    5. Chart Formatting and Styling

      • Custom Themes
      • Coordinate Flipping
      • Annotations

    Project Structure

    The FILETREE holds the following folders and files:

    1. data: It contains the data.csv file which has the following columns:
      • patient_id
      • patient_name
      • gender
      • date_of_birth
      • visit_date
      • symptoms
      • diagnosis
      • medication
      • dosage
    2. src: It contains the following four files where you will write your code:
      • step1.R
      • step2.R
      • step3.R
      • step4.R
    3. Rplots.pdf: Whenever you generate a plot, it will be saved inside this PDF file. You can download it to view the resultant plot.

    All set, let’s move! 🏃🏻‍♀️‍➡️

  2. Challenge

    Creating basic charts

    Your first lab objective includes a series of tasks, ranging from loading the dataset to creating bar charts, line charts, and histograms.

    For this step, you will work in the src/step1.R file, which contains the required libraries and a pre-filled data_with_month function to assist you.

    Begin with the first task to kick off your journey! > ℹ️ NOTE

    You can save R files with either the .r or .R extension, as R recognizes both as valid script formats. However, if you attempt to reference a xyz.r file within an abc.r file using xyz.R, it will fail because R treats the extensions as different.

  3. Challenge

    Customize charts

    In this step, you will customize the bar chart and line chart that you have created in the step1.R file.

    You will work in the src/step2.R file, which already includes the ggplot2 library and the src/step1.R file. This file retrieves the data frame using the load_data function and stores the result in the data variable.

  4. Challenge

    Create multivariate charts

    Up to this point, you have created charts that rely on a single column. Now, you will explore how to build charts using multiple variables.

    For this step, you will work in the src/step3.R file, which includes the required library and imports. This file also contains two pre-defined functions:

    1. filtered_data – Returns a subset of the data frame containing only four diagnoses: Viral Infection, Chronic Insomnia, Hay Fever, and Allergic Rhinitis.
    2. add_age_month – Returns a data frame with the corresponding patient age and visit month.
  5. Challenge

    Format and style charts

    In the final step, you will learn to format and style charts. You will perform the tasks inside the src/step4.R file which has the necessary library and imports. In this lab, you explored how to create both basic and multivariate charts, from simple plots to those with multiple subplots. Additionally, you learned how to enhance their aesthetics for better readability and tailor them to your specific needs.

    Keep up the learning! ✌

Written content author.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.