The two previous posts described how you can make histograms with basic R and the
ggplot2 package. This third and last part of our histograms tutorial will look at
ggvis. This package is similar to
ggplot2, as it is also based on “the grammar of graphics”. However,
ggvis has slightly different expressions and extends
ggplot2 by adding new features to make your plots interactive. Want to learn more? Discover the DataCamp tutorials.
Step One. Get The ggvis Package into RStudio
To start off plotting histograms in
ggvis, you first need to load the
ggvis is not yet installed on your system, you’ll have to do this first. This can easily be done through
Step Two. Your Data
It might seem silly, but do not forget about your data! You can use a data set that is built into R already or get your own data set. In this example, we will continue with the data from the previous blog post on histograms: the
chol data set.
If you’re new to this tutorial, you can load in the data set through the
url() function, embedded into the
Step Three. Start Plotting Basic Histograms
To make a basic histogram, you can use the following line of code to make a simple histogram with the “AGE” column from the “chol” data frame:
Study the line of code printed above a bit more clearly: you see that ggvis uses the operator
%>%, which is also known as the pipe operator, from the
magrittr package. This operator passes the result from its left-hand side into the first argument of the function on its right-hand side. So
f(x) %>% g(y) is actually a shortcut for
As an extra example, consider the two following R commands, that are completely equivalent:
option1 <- sum(abs(-3:3)) option2 <- -3:3 %>% abs() %>% sum()
Step Four. Prettify your Histograms
Similarly to the
hist() function and functions of
ggplot2(), you can easily adapt the visualization of your histogram by extending the original code. In this case, the original line of code could be extended as follows to visualize a histogram that takes the “AGE” column from the “chol” data set, fill the bins up with a red color and makes bins of width 5, with a zero center and add age as a title to the x-axis:
Just like with the
hist() function and ggplot2, you can break up this rather large chunk of code to see what each small piece contributes to the visualization during the plotting of the histogram:
You can easily adjust the width of the bins by changing the
width argument inside the
width argument already set the bin width to 5, but where do bins start and where do they end?
You can use the
boundary argument for this.
center should refer to one of the bins’ center value, which automatically determines the other bins location. The
boundary argument specifies the boundary value of one of the bins. Here again, specifying a single value fixes the location of all bins. As these two arguments specify the same thing in a different way, you should set at most one of
As an example, compare the previous histogram (with for example 35 as a bin center) to the following histogram, where
center is set to 36:
You can enforce the same plot by setting the
boundary argument to 33.5. This number equals 36 minus half the bin width:
Note that the boundary and center may be outside the range of the data. In that case,
ggvis is smart enough to determine to extrapolate what you meant and will decide on a location of the bins. Experiment with the arguments described above to see what the influence could be on the interpretation of the histogram.
By using the pipe operator in addition to
add_axis(), you can specify which axis you want to give a particular title or label. In this case, we put
Similarly, you can also label the y-axis:
You can fill up the bins with any color that you would like by using
Note the use of
:=, which sets a property to a specific color (or size, width, etc.), as opposed to mapping it, as the
= operator does.
Step Five. Adding Basic Interactivity to your Histograms
There are many advantages of working with ggvis, but one that definitely stands out is the fact that you can actually make your histograms interactive! This allows readers of your reports to change the parameters of your plots and see the results of these changes instantaneously.
ggvis makes use of
shiny for this. Let’s go into the basics of the interactivity that you can add to your histograms.
For example, you can add an input slider that lets you decide on the width of your bins. Here’s how:
Note that the figure that is shown in this post is a static version of the histogram. To check the interactive plot, that is backed with an R process to change the graphics on the fly, visit the histogram on our Shiny server.
The input values that are given to
input_slider are those that you can also see when you execute the code in the RStudio console. In this case, you would see a slider that ranges from values 1 to 10 with steps of 1 and that has a label
Next, you can also add a different type of user input, a select box, to determine the fill color of the bins:
The interactive version of this histogram can again be found on our Shiny server. Here, the
fill property is set to one of four choices, specified in the
choices argument inside
selected specified that the default color is blue, while
label shows the label
Fill Color in the interactive plot.
Note that the shiny package is needed for these interactive visualizations. Normally, RStudio comes with this package by default. If you are not working in RStudio, install
shiny by executing
Remember to keep in mind what you want to achieve with your histogram and how you want to achieve this! Sometimes, a static representation might be better than a dynamic one.
Step Six. Using ggvis for so much more
This section was just the tip of the iceberg! Believe us when we say that you can do so much more with this package.
Tip: If you would like to have a more detailed and broader understanding of data visualization in R, you might be interested in the Data Visualization with R skill track, in which you’ll learn how to communicate the essential features of your data by creating beautiful visualizations with ggplot2 and base R graphics.
There are a lot of options to make histograms in R. The option you should choose is really a trade-off between what you want to accomplish and how fast you want to achieve this. But of course, this trade-off is subject to your own programming experience with R and other languages: either option would go fast for any well-trained programmer.
However, if you are just starting with R, it might be a good idea to keep this tutorial at hand. Additionally, we encourage you to go and check out the additional information that you can find in the tutorial and below. This way, you will gradually be submerged into the field of data visualization, which will give you the time to get fluent very quickly.
So, in the end, the only thing that remains of the original trade-off and that probably is the most important thing to take into account is the question “What do you want to achieve with your histogram?”.
Exploratory Data Analysis in R
Visualization Best Practices in R
How to Become a Data Scientist in 8 StepsFind out everything you need to know about becoming a data scientist, and find out whether it’s the right career for you!
Predicting FIFA World Cup Qatar 2022 WinnersLearn to use Elo ratings to quantify national soccer team performance, and see how the model can be used to predict the winner of FIFA World Cup Qatar 2022.
Arne Warnke •
How Data Science is Changing SoccerWith the Fifa 2022 World Cup upon us, learn about the most widely used data science use-cases in soccer.
ggplot2 Cheat Sheetggplot2 is considered to be one of the most robust data visualization packages in any programming language. Use this cheat sheet to guide your ggplot2 learning journey.
DataCamp Team •