banner



How To Draw Histogram In Java

Create ggplot2 Histogram in R (7 Examples) | geom_histogram Function

This page shows how to create histograms with the ggplot2 package in R programming.

The tutorial will contain the following:

  • Cosmos of Case Data & Setting Up ggplot2 Packet
  • Example one: Bones ggplot2 Histogram in R
  • Example two: Primary Title & Axis Labels of ggplot2 Histogram
  • Example 3: Colors of ggplot2 Histogram
  • Instance 4: Bar Width of ggplot2 Histogram
  • Instance five: Axis Limits of ggplot2 Histogram
  • Example 6: Density & Histogram in Same ggplot2 Plot
  • Example seven: Multiple Histograms in Same ggplot Plot
  • Video, Farther Resource & Summary

Let's dive into it.

Cosmos of Example Data & Setting Up ggplot2 Package

In the examples of this R tutorial, we'll apply the following random case information:

                set                .                seed                (                5753                )                # Create example information                information                <-                data.                frame                (10                =                rnorm(                thou                )                )              

set.seed(5753) # Create example information data <- data.frame(x = rnorm(1000))

Furthermore, we need to install and load the ggplot2 R parcel:

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

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

Instance 1: Basic ggplot2 Histogram in R

If nosotros desire to create a histogram with the ggplot2 package, nosotros need to use the geom_histogram office. The R code of Example 1 shows how to draw a bones ggplot2 histogram.

ggplot(information, aes(ten                =                x)                )                +                # Bones ggplot2 histogram                geom_histogram(                )              

ggplot(data, aes(x = ten)) + # Basic ggplot2 histogram geom_histogram()

ggplot2 Histogram in R Figure 1

Figure 1: Basic ggplot2 Histogram in R.

Figure 1 visualizes the output of the previous R syntax: A histogram in the typical design of the ggplot2 package.

In the following examples I'll explain how to alter this basic histogram representation. And so keep on reading!

Example 2: Primary Title & Axis Labels of ggplot2 Histogram

In ggplot2, we can modify the main title and the axis labels of a graphic every bit shown below:

ggplot(data, aes(ten                =                10)                )                +                # Alter title & axis labels                geom_histogram(                )                +                labs(title                =                "My ggplot2 Histogram",        x                =                "Values",        y                =                "Count of Values"                )              

ggplot(data, aes(x = 10)) + # Modify championship & axis labels geom_histogram() + labs(title = "My ggplot2 Histogram", x = "Values", y = "Count of Values")

ggplot2 Histogram in R Figure 2

Figure two: Modified Main Title & Axis Labels.

Figure two shows the same histogram every bit Figure ane, only with a manually specified chief championship and user-defined centrality labels.

Example 3: Colors of ggplot2 Histogram

This example shows how to modify the colors of our ggplot2 histogram in R. If we want to modify the colour around the bars, nosotros have to specify the col argument inside the geom_histogram office:

ggplot(data, aes(10                =                10)                )                +                # Modify color effectually bars                geom_histogram(col                =                "cerise"                )              

ggplot(information, aes(ten = ten)) + # Modify color around confined geom_histogram(col = "cerise")

ggplot2 Histogram in R Figure 3

Effigy 3: Modified Color of Histogram.

If we desire to change the color of the bars, nosotros accept to specify the fill argument within the geom_histogram role. We tin can also specify the col statement to a different color than the fill up argument:

ggplot(data, aes(x                =                x)                )                +                # Change filling of confined                geom_histogram(col                =                "black", make full                =                "ruddy"                )              

ggplot(data, aes(ten = x)) + # Modify filling of bars geom_histogram(col = "black", fill = "red")

ggplot2 Histogram in R Figure 4

Figure 4: Modified Filling Color of Histogram.

Instance 4: Bar Width of ggplot2 Histogram

The geom_histogram control also provides the possibility to adjust the width of our histogram bars. Nosotros simply have to specify the binwidth option as shown below:

ggplot(data, aes(x                =                x)                )                +                # Alter width of bars                geom_histogram(binwidth                =                0.i                )              

ggplot(data, aes(10 = x)) + # Modify width of confined geom_histogram(binwidth = 0.i)

ggplot2 Histogram in R Figure 5

Effigy five: Changing Bar Width in ggplot2 Histogram.

Every bit you can see based on Figure 5, the confined of our new histogram are thinner.

Example five: Axis Limits of ggplot2 Histogram

If nosotros desire to zoom in or zoom out, we can adapt the axis limits with the xlim and ylim functions:

ggplot(data, aes(ten                =                x)                )                +                # Modify ten- & y-centrality limits                geom_histogram(                )                +                xlim(                -                4,                ane                )                +                ylim(                0,                100                )              

ggplot(data, aes(ten = x)) + # Modify x- & y-centrality limits geom_histogram() + xlim(- 4, 1) + ylim(0, 100)

ggplot2 Histogram in R Figure 6

Figure vi: Cutting Off Certain Parts of the Histogram by Setting User-Divers Axis Limits.

Figure 6 shows the output of the previous R code. We increased the height of the y-axis and moved the x-axis to the left. Note that some values on the left side of our histogram were cut off.

Example 6: Density & Histogram in Same ggplot2 Plot

We tin also overlay our histogram with a probability density plot. For this task, nosotros need to specify y = ..density.. inside the aesthetics of the geom_histogram office and we as well need to add another line of code to our ggplot2 syntax, which is drawing the density plot:

ggplot(information, aes(10                =                x)                )                +                # Draw density in a higher place histogram                geom_histogram(aes(y                =                ..                density                ..                )                )                +                geom_density(blastoff                =                0.1, fill                =                "red"                )              

ggplot(data, aes(x = x)) + # Draw density in a higher place histogram geom_histogram(aes(y = ..density..)) + geom_density(alpha = 0.one, make full = "red")

ggplot2 Histogram in R Figure 7

Figure 7: Overlay Histogram with Density in Aforementioned Graphic.

Annotation that we take specified within the geom_density function that the density plot should be transparent and filled with the color crimson. This helps to distinguish between the histogram in the background and the overlaying density plot.

Instance 7: Multiple Histograms in Same ggplot Plot

Like to Case 6, we can draw multiple histograms in the same ggplot2 graph. Consider the following data frame:

                set                .                seed                (                19191                )                # Create example information with grouping                data2                <-                information.                frame                (ten                =                c(rnorm(                500                ), rnorm(                500,                three,                two                )                ),                grouping                =                as                .                factor                (c(rep(                i,                500                ), rep(                2,                500                )                )                )                )              

set.seed(19191) # Create example data with group data2 <- data.frame(x = c(rnorm(500), rnorm(500, 3, 2)), group = equally.factor(c(rep(1, 500), rep(ii, 500))))

Our new data contains an additional group column. Now nosotros can depict 2 histograms in the aforementioned plot by separating our values by the group variable:

ggplot(data2, aes(x                =                x, fill                =                group                )                )                +                # Draw two histograms in aforementioned plot                geom_histogram(blastoff                =                0.5, position                =                "identity"                )              

ggplot(data2, aes(x = x, make full = group)) + # Draw ii histograms in same plot geom_histogram(blastoff = 0.5, position = "identity")

ggplot2 Histogram in R Figure 8

Figure eight: Draw Several Histograms in One Graph.

Video, Further Resources & Summary

Accept a expect at the post-obit video which I take published on my YouTube aqueduct. I explicate the R codes of this folio in the video.

The YouTube video volition be added soon.

In addition to the video, y'all could take a expect at the related articles on this website.

  • Create a Histogram in Base R
  • Draw Multiple Overlaid Histograms with ggplot2 Package in R
  • R Graphics Gallery
  • The R Programming Linguistic communication

In summary: Y'all learned in this article how to make a histogram with the ggplot2 parcel in the R programming language. Don't hesitate to let me know in the comments below, in example you take any additional questions.

Source: https://statisticsglobe.com/ggplot2-histogram-in-r-geom_histogram-function

Posted by: reisingermonce1983.blogspot.com

0 Response to "How To Draw Histogram In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel