banner



How To Put Two Plots Side By Side In R Shiny

Draw Multiple ggplot2 Plots Side-past-Side (R Programming Example)

In this R programming tutorial y'all'll learn how to describe multiple ggplots side-past-side.

The article is structured as follows:

  • Create Example Information
  • Create & Shop Multiple ggplots
  • Draw Multiple ggplots Side-by-Side
  • Video & Farther Resource

So without further ado, so let's get straight to the example.

Create Case Data

Starting time, we need to create some instance data for the creation of our plots. Consider the following ii data frames:

                set up                .                seed                (                5645                )                # Fix seed                data1                <-                data.                frame                (10                =                rnorm(                500                )                )                # Create information for commencement plot                data2                <-                information.                frame                (x                =                rnorm(                1000                ),                # Create data for second plot                y                =                rnorm(                1000                )                )              

set.seed(5645) # Set up seed data1 <- data.frame(x = rnorm(500)) # Create data for showtime plot data2 <- data.frame(ten = rnorm(thou), # Create data for second plot y = rnorm(thou))

Each of the information frames (i.e. data1 and data2) contains the values for one plot. Now let's create these plots…

Create & Store Multiple ggplots

Before we can create plots with the ggplot2 package, we need to install and load the packet to R:

install.                packages                (                "ggplot2"                )                # Install ggplot2 package                library(                "ggplot2"                )                # Load ggplot2 package              

install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 bundle

Now, we tin can create two ggplots with the post-obit R code:

ggp1                <-                ggplot(data1, aes(10                =                x)                )                +                # Create first plot                geom_density(                )                ggp2                <-                ggplot(data2, aes(x                =                ten, y                =                y)                )                +                # Create 2d plot                geom_point(                )              

ggp1 <- ggplot(data1, aes(10 = x)) + # Create first plot geom_density() ggp2 <- ggplot(data2, aes(x = x, y = y)) + # Create 2d plot geom_point()

The information object ggp1 contains a density plot and the data object ggp2 contains a scatterplot.

Notation that nosotros could shop any type of graphic or plot in these data objects. No thing if we want to draw a histogram using the geom_histogram office, a barchart using the geom_bar function, a QQplot or any other ggplot, but store it in such a information object.

Furthermore, you are free to create as many different images as y'all want. The post-obit part of this R tutorial volition testify you how to draw as many different ggplots too each other as you lot want…

Draw Multiple ggplots Side-by-Side

In order to print several ggplot graphs side-past-side, we need to install and load the gridExtra R packet:

install.                packages                (                "gridExtra"                )                # Install gridExtra package                library(                "gridExtra"                )                # Load gridExtra package              

install.packages("gridExtra") # Install gridExtra packet library("gridExtra") # Load gridExtra package

The gridExtra package contains the filigree.accommodate role. Nosotros can use this function to return our two example plots within the same plot window:

grid.                adjust                (ggp1, ggp2, ncol                =                ii                )                # Apply filigree.accommodate role              

grid.arrange(ggp1, ggp2, ncol = 2) # Use grid.arrange function

ggplots besides each other

Figure 1: Ii ggplots Side-by-Side.

Note that nosotros have specified inside the grid.arrange function that we would like to combine the plots in ii columns. We could also specify ncol = 1 to return the two plots above each other. In case we would have more than two pictures we could adapt and mix these graphics with the ncol and nrow arguments of the grid.adjust function as we want.

Of crusade we could also export the created multi-plot as PDF, PNG, JPEG or whatsoever other file format that is supported by R (or RStudio).

Video & Further Resources

If yous need further explanations on the creation of side-by-side ggplots yous could have a expect at the post-obit video on my YouTube channel. In the video, I'm showing the example of this page in a live session:

If you desire to learn more about the ggplot2 parcel in general, you could also have a look at the other R tutorials of my homepage. You can find a list of interesting tutorials below:

  • Change Legend Title in ggplot2
  • Alter Position of ggplot Title
  • Remove Legend in ggplot2
  • Set Centrality Limits in ggplot2 R Plot
  • Adapt Plots Using the layout Part in R
  • R Functions Listing (+ Examples)
  • The R Programming Language

I hope you learned in this tutorial how to lay out multiple ggplots showing unlike types of plots and different variables on the same page and canvas. However, if you have any further questions on how to combine several plots, then please let me know in the comments below.

Source: https://statisticsglobe.com/draw-multiple-ggplot-plots-side-by-side

Posted by: hinesableatifes69.blogspot.com

0 Response to "How To Put Two Plots Side By Side In R Shiny"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel