Open In App

Printing plots generated in a function using R Markdown in RStudio

Last Updated : 16 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will explore how to generate and print the plots created within the function using R Markdown in RStudio. R Markdown is a powerful tool for creating dynamic documents that integrate code, results, and narrative text. We will demonstrate how to create the plot within the R function and then print that plot in an R Markdown document.

Prerequisites

  • Basic Knowledge of R programming.
  • Familiarity with R Markdown.
  • R and RStudio are installed in your local system.

Creating a Plot in a Function

We can create a simple function in the R that generates a plot. We will use the ggplot2 package for creating the plot.

R
# Install and load the ggplot2 package
install.packages("ggplot2")
library(ggplot2)

# Define a function that creates a plot
create_plot <- function(data) {
  p <- ggplot(data, aes(x = wt, y = mpg)) +
    geom_point() +
    theme_minimal()
  return(p)
}

# Example data
data <- mtcars

# Create and print the plot
plot <- create_plot(data)
print(plot)

Output:

mark1
Printing plots generated in a function using R Markdown in RStudio

Printing plots generated in a function using R Markdown

R Markdown can allows you to create the documents that certain the mixture of the markdown text and embedded R code. To create the R Markdown document in the RStudio.

  1. Go to File > New File > R Markdown.
  2. File the title and author and select the output format.
  3. Click ok
mark2
Printing plots generated in a function using R Markdown in RStudio

Printing the Plots in R Markdown

To print the plots generated within the function in an R Markdown documents paste this the R Markdown section:

---
title: "Printing Plots in R Markdown"
author: "syam"
date: "2024-07-14"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
R
---
title: "Printing Plots in R Markdown"
author: "syam"
date: "2024-07-14"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML,
PDF, and MS Word documents. For more details on using R Markdown 
see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes 
both content as well as the output of any embedded R code chunks within the document.
You can embed an R code chunk like this:


## Including Plots

You can also embed plots, for example:

```{r pressure, echo=TRUE}
# Load the ggplot2 package
library(ggplot2)

# Define a function that creates a plot
create_plot <- function(data) {
  p <- ggplot(data, aes(x = wt, y = mpg)) +
    geom_point() +
    theme_minimal()
  return(p)
}

# Example data
data <- mtcars

# Create and print the plot
plot <- create_plot(data)
print(plot)

```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of 
the R code that generated the plot.

Output:

R Markdown HTML Result

mark3
Printing plots generated in a function using R Markdown in RStudio
mark4
Printing plots generated in a function using R Markdown in RStudio

Conclusion

Using the R Markdown, we can seamlessly integrate the R code, results and narrative text into the single document. This article demonstrated how to create and print the plots generated within the function using R Markdown in RStudio.


Next Article

Similar Reads