WHT

How To Plot Multiple Histograms In R | How To Draw Multiple Histograms Together In R Using Ggplot2? | Statswithr | Arnab Hazra

Statswithr

Subscribe Here

Likes

12

Views

756

How To Draw Multiple Histograms Together In R Using Ggplot2? | Statswithr | Arnab Hazra

Transcript:

Hello, friends. We are back with our new video. In this video, I will discuss how to draw multiple histograms together in R or Rstudio, using ggplot2 that you can use for your official presentation or in your publication. Please visit our website statswithrcom for more details. And if you have any queries, shoot us an email at learnstatisticswithr@gmailcom. All the codes we discuss here are available in the video description. Feel free to copy paste, run and learn. Just in case you want to draw a single histogra’m. Please watch our video-. How to create a nice histogram in R using ggplot2? The overall process involves several steps and I will discuss stepwise. Let’s begin! I assume that you know how to load your data into R or Rstudio. Just in case you don’t know and want to read the data from an excel sheet. Please watch our video-. How to create a nice bar chart in R using ggplot2, for example. For today’s discussion, we simulate data from two normal distributions and assume that they represent the heights of one thousand adult males randomly selected from the countries- United States and India. Before we move to create the ggplot’s first. We need the package ggplot2. So we need to install it and call it into R or RStudio. For these, we use the commands, installpackages and library. Now we are ready to create the ggplot. In the console Window Type Ggplot, a blank bracket plus geom_histogram within aesthetics. X equals to US. Comma fill equals to blue within quotes plus geom_histogram within aesthetics. X equals to Ind, fill equals to darkgreen- within quotes. If you want some other colors, choose accordingly. Now, press enter, Okay. You get your first multiple histogram. There are many things that we need to change. Let’s do step by step First. Often, we want to plot the proportions or probabilities on the Y-axi’s instead of the counts. If you don’t want to do that, you can skip this step. To do that within aesthetics, add the argument Y equals to density for each of the geom_histogram statements. Now, press enter, Okay. You get your second multiple histogram. You can see that the Y-axis has changed from counts to proportions or densities Next. So far, you have seen a warning message-. Stat_bin using bins equals to 30. Pick better value with bandwidth. Unlike a simple histogram using the function HIST, in the graphics package here, the bins are not selected automatically that satisfies some reasonable criteria. Suppose you want 20 bins instead of 30. To do that add. The argument- bins equals to 20 within each geom_histogram statement outside aesthetics. Now, press enter, Okay. You get your third multiple histogram. Both the histograms have 20 bins. The binning of geom_histogram is different from the function hist in the graphics package Now. If you want to allow the same binning as HIST replace the argument bins = 20 to breaks = hist(US plot = FALSE)$breaks and same for IND. If you don’t set plot = FALSE within HIST, it will plot additional histograms which we don’t need. Now, press enter, Okay. You get your fourth multiple histogram. You can see that the binning has changed. Next note that one histogram is not clearly visible as it is hidden behind the other one. To fix this add Alpha = 0.7 within geom_histogram outside aesthetics for both the cases. Also note that we are storing everything so far into p0 and print(p0) returns the updated multiple histogram. We won’t change these steps and we’ll add the subsequent layers on the top of p0 Alternatively, you can just keep on adding. Here we just do it for convenience. Now, press enter, Okay. You get your fifth multiple histogram. You can see that. The histogram in the back is now visible. Next, as we have already said that we assume that the data sets correspond to 1000 adult males in US and India each. You are probably interested to replace the level of the X-axis with say height in centimeter. Also, you may want to capitalize the initial of the label of the Y-axis. To do that, add the line. Xlab-height in centimeters within quotes plus ylab-density with capitalized D. Now, press enter, Okay. You get your sixth multiple histogram. You can see the changes in the labels. Next you might be interested to add a title, say Comparison of height distributions Also. You might want to place the title at the top-center. To do these add two lines. First line is ggtitle comparison of height distributions within Quotes-. This adds the title. The default position of the title is left-aligned. To place the title at the center, add the Line- theme(plottitle = element_text(hjust = 05)). Now, press enter, Okay. You get your seventh multiple histogram. You can see that the figure-title is created now. And also it is at the top center. Next you may feel that the fonts of the title, the axes and axis-levels are small and you may want to enlarge them. To do them within theme, add some arguments. First for plottitle add size = 15 within element_text. This increases the font of the plot-title. Also add the Lines- Axistext = Element_text(size = 15)- This increases the font of the axes Axistitle = element_text(size =15)-. This increases the font of the axis-titles. If you don’t want one or two of them, you can remove that line. The larger the size you want, you need to fix a larger number. So choose a number accordingly. Also note that we are storing everything so far into p1 and print(p1) returns the updated multiple histogram. We will add the subsequent layers on the top of p1 Now, press enter, Okay. You get your eighth multiple histogram. You can see that the title, the axes and the axis-levels are larger now. Next note that the colors of the histograms are not at all the colors we wanted. To fix this add the line- scale_fill_manual values = a vector of colors-, blue and darkgreen. All the color names are within quotes. Now, press enter, Okay. You get your ninth multiple histogram. Note that the colors have changed to the colors we wanted. Next note that the title and the labels of the legend are not very informative. Suppose we want to change the legend-title to say Country and the labels to say US. And India, respectively. To do that within scale_fill_manual add the line- name = Country within quotes. This adds the legend-title and also add another line- labels = a vector with elements, US and India both within quotes. Now, press enter, Okay. You get your tenth multiple histogram. You can see that the legend-title and the labels have changed now. Next note that the legend-title and the legend-labels are quite small To enlarge them within theme. Add the lines legend text = element_text(size = 15)- This increases the labels of the legend Legendtitle = Element_text(size = 15)- This increases the title of the legend. Now, press enter, Okay. You get your final multiple histogram. You might consider this figure for your presentation or in your publication. If you are interested to learn how to draw multiple boxplots together using ggplot2. Watch our video-. How to draw multiple boxplots together in R using ggplot2. If you found this video helpful, please like comment share and subscribe to my channel and press on the bell icon for moreupdates.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more