Tutorials
r programming
+1

How to Make a Histogram with ggvis in R

Learn how to make a histogram with ggvis. Go from the very basics to creating interactive graphs with shiny to display distributions.

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 package:

library(ggvis)

If ggvis is not yet installed on your system, you’ll have to do this first. This can easily be done through

install.packages("ggvis")

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 blogpost 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 read.table() function:

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJjaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkifQ==


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:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiIjIFNpbXBsZSBoaXN0b2dyYW0gd2l0aCBgQUdFYCBkYXRhXG5jaG9sICU+JSBcbiAgZ2d2aXMofkFHRSkgJT4lIFxuICBsYXllcl9oaXN0b2dyYW1zKCkifQ==


basic histogram in R

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 g(f(x), y).

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:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiIjIEhpc3RvZ3JhbSBvZiBgQUdFYFxuY2hvbCAlPiUgXG4gIGdndmlzKH5BR0UpICU+JVxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gNSwgY2VudGVyID0gMzUsIGZpbGwgOj0gXCIjRTc0QzNDXCIpICU+JSAgXG4gIGFkZF9heGlzKFwieFwiLCB0aXRsZSA9IFwiQWdlXCIpJT4lICBcbiAgYWRkX2F4aXMoXCJ5XCIsIHRpdGxlID0gXCJDb3VudFwiKSJ9


complete histogram

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:

Bins

You can easily adjust the width of the bins by changing the width argument inside the layer_histograms() function:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiJjaG9sICU+JSBcbiAgZ2d2aXMofkFHRSkgJT4lXG4gIC4uLi4uLi4uLi4uLi4uLi4od2lkdGggPSA1KSIsInNvbHV0aW9uIjoiY2hvbCAlPiUgXG4gIGdndmlzKH5BR0UpICU+JVxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gNSkiLCJzY3QiOiJleCgpICU+JSBjaGVja19vYmplY3QoXCJjaG9sXCIpXG5leCgpICU+JSBjaGVja19mdW5jdGlvbihcImdndmlzXCIpXG5leCgpICU+JSBjaGVja19mdW5jdGlvbihcImxheWVyX2hpc3RvZ3JhbXNcIikgJT4lIGNoZWNrX2FyZygnd2lkdGgnKSJ9


R histogram binwidth

The width argument already set the bin width to 5, but where do bins start and where do they end?

You can use the center or 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 center or boundary.

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:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiJjaG9sICU+JSBcbiAgZ2d2aXMofkFHRSkgJT4lIFxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gNSwgLi4uLi4uLi4uLi4pIiwic29sdXRpb24iOiJjaG9sICU+JSBcbiAgZ2d2aXMofkFHRSkgJT4lIFxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gNSwgY2VudGVyID0gMzYpIiwic2N0IjoiZXgoKSAlPiUgY2hlY2tfb2JqZWN0KFwiY2hvbFwiKVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJnZ3Zpc1wiKVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJsYXllcl9oaXN0b2dyYW1zXCIpICU+JSBjaGVja19hcmcoJ3dpZHRoJykgJT4lIGNoZWNrX2FyZygnY2VudGVyJykifQ==


histogram R center

You can enforce the same plot by setting the boundary argument to 33.5. This number equals 36 minus half the bin width:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiJjaG9sICU+JSBcbiAgZ2d2aXMofkFHRSkgJT4lIFxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gNSwgLi4uLi4uLi4uLi4uLi4uKSIsInNvbHV0aW9uIjoiY2hvbCAlPiUgXG4gIGdndmlzKH5BR0UpICU+JSBcbiAgbGF5ZXJfaGlzdG9ncmFtcyh3aWR0aCA9IDUsIGJvdW5kYXJ5ID0gMzMuNSkiLCJzY3QiOiJleCgpICU+JSBjaGVja19vYmplY3QoXCJjaG9sXCIpXG5leCgpICU+JSBjaGVja19mdW5jdGlvbihcImdndmlzXCIpXG5leCgpICU+JSBjaGVja19mdW5jdGlvbihcImxheWVyX2hpc3RvZ3JhbXNcIikgJT4lIGNoZWNrX2FyZygnd2lkdGgnKSAlPiUgY2hlY2tfYXJnKCdib3VuZGFyeScpIn0=


histogram boundary

Note that the boundary and center may be outside the range of the data. In that case ggvis is smart enough to determine 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.

Names/colors

By using the pipe operator in addition to add_axis(), you can specify which axis you want to give a certain title or label. In this case, we put

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiJjaG9sICU+JSBcbiAgZ2d2aXMofkFHRSkgJT4lIFxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gNSwgY2VudGVyID0gMzUpICU+JVxuICAuLi4uLi4uLi4oXCJ4XCIsIHRpdGxlID0gXCJBZ2VcIikiLCJzb2x1dGlvbiI6ImNob2wgJT4lIFxuICBnZ3Zpcyh+QUdFKSAlPiUgXG4gIGxheWVyX2hpc3RvZ3JhbXMod2lkdGggPSA1LCBjZW50ZXIgPSAzNSkgJT4lXG4gIGFkZF9heGlzKFwieFwiLCB0aXRsZSA9IFwiQWdlXCIpIiwic2N0IjoiZXgoKSAlPiUgY2hlY2tfb2JqZWN0KFwiY2hvbFwiKVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJnZ3Zpc1wiKVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJsYXllcl9oaXN0b2dyYW1zXCIpICU+JSBjaGVja19hcmcoJ3dpZHRoJykgJT4lIGNoZWNrX2FyZygnY2VudGVyJylcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiYWRkX2F4aXNcIikgJT4lIGNoZWNrX2FyZygndmlzJykgJT4lIGNoZWNrX2FyZygndGl0bGUnKSJ9


histogram with x axis

Similarly, you can also label the y-axis:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJh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KVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJhZGRfYXhpc1wiLCBpbmRleCA9IDEpICU+JSBjaGVja19hcmcoJ3ZpcycpICU+JSBjaGVja19hcmcoJ3RpdGxlJylcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiYWRkX2F4aXNcIiwgaW5kZXggPSAyKSAlPiUgY2hlY2tfYXJnKCd2aXMnKSAlPiUgY2hlY2tfYXJnKCd0aXRsZScpIn0=


histogram with fill

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.

Tip: you can find more information about this in DataCamp’s ggvis course.

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:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiJjaG9sICU+JVxuICBnZ3Zpcyh+QUdFKSAlPiVcbiAgbGF5ZXJfaGlzdG9ncmFtcyguLi4uLiA9IGlucHV0X3NsaWRlcigxLCAxMCwgc3RlcCA9IDEsIGxhYmVsID0gXCJCaW4gV2lkdGhcIiksIFxuICAgICAgICAgICAgICAgICAgIGNlbnRlciA9IDM1LCBcbiAgICAgICAgICAgICAgICAgICBmaWxsIDo9IFwiI0U3NEMzQ1wiKSAlPiUgXG4gIC4uLi4uLi4uKFwieFwiLCB0aXRsZSA9IFwiQWdlXCIpJT4lICBcbiAgYWRkX2F4aXMoXCJ5XCIsIHRpdGxlID0gXCJCaW4gQ291bnRcIikiLCJzb2x1dGlvbiI6ImNob2wgJT4lXG4gIGdndmlzKH5BR0UpICU+JVxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gaW5wdXRfc2xpZGVyKDEsIDEwLCBzdGVwID0gMSwgbGFiZWwgPSBcIkJpbiBXaWR0aFwiKSwgXG4gICAgICAgICAgICAgICAgICAgY2VudGVyID0gMzUsIFxuICAgICAgICAgICAgICAgICAgIGZpbGwgOj0gXCIjRTc0QzNDXCIpICU+JSBcbiAgYWRkX2F4aXMoXCJ4XCIsIHRpdGxlID0gXCJBZ2VcIiklPiUgIFxuICBhZGRfYXhpcyhcInlcIiwgdGl0bGUgPSBcIkJpbiBDb3VudFwiKSIsInNjdCI6ImV4KCkgJT4lIGNoZWNrX29iamVjdChcImNob2xcIilcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiZ2d2aXNcIilcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwibGF5ZXJfaGlzdG9ncmFtc1wiKSAlPiUgY2hlY2tfYXJnKCd3aWR0aCcpICU+JSBjaGVja19hcmcoJ2NlbnRlcicpICU+JSBjaGVja19hcmcoJ2ZpbGwnKVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJhZGRfYXhpc1wiLCBpbmRleCA9IDEpICU+JSBjaGVja19hcmcoJ3ZpcycpICU+JSBjaGVja19hcmcoJ3RpdGxlJylcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiYWRkX2F4aXNcIiwgaW5kZXggPSAyKSAlPiUgY2hlY2tfYXJnKCd2aXMnKSAlPiUgY2hlY2tfYXJnKCd0aXRsZScpIn0=


interactive histogram 1

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 Bin Width.

Next, you can also add a different type of user input, a select box, to determine the fill color of the bins:

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZ2d2aXMpXG5jaG9sIDwtIHJlYWQudGFibGUodXJsKFwiaHR0cDovL3MzLmFtYXpvbmF3cy5jb20vYXNzZXRzLmRhdGFjYW1wLmNvbS9ibG9nX2Fzc2V0cy9jaG9sLnR4dFwiKSwgaGVhZGVyID0gVFJVRSkiLCJzYW1wbGUiOiJjaG9sICU+JVxuICBnZ3Zpcyh+QUdFKSAlPiVcbiAgLi4uLi4uLi4uLi4uLi4uLih3aWR0aCA9IGlucHV0X3NsaWRlcigxLCAxMCwgc3RlcCA9IDEsIGxhYmVsID0gXCJCaW4gV2lkdGhcIiksIFxuICAgICAgICAgICAgICAgICAgIGNlbnRlciA9IDM1LCBcbiAgICAgICAgICAgICAgICAgICBmaWxsIDo9IGlucHV0X3NlbGVjdChjaG9pY2VzID0gYyhcInJlZFwiLCBcImdyZWVuXCIsIFwiYmx1ZVwiLCBcInllbGxvd1wiKSwgXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VsZWN0ZWQgPSBcImJsdWVcIiwgbGFiZWwgPSBcIkZpbGwgQ29sb3JcIikpICU+JSBcbiAgYWRkX2F4aXMoXCJ4XCIsIHRpdGxlID0gXCJBZ2VcIikgJT4lICBcbiAgYWRkX2F4aXMoXCJ5XCIsIHRpdGxlID0gXCJCaW4gQ291bnRcIikiLCJzb2x1dGlvbiI6ImNob2wgJT4lXG4gIGdndmlzKH5BR0UpICU+JVxuICBsYXllcl9oaXN0b2dyYW1zKHdpZHRoID0gaW5wdXRfc2xpZGVyKDEsIDEwLCBzdGVwID0gMSwgbGFiZWwgPSBcIkJpbiBXaWR0aFwiKSwgXG4gICAgICAgICAgICAgICAgICAgY2VudGVyID0gMzUsIFxuICAgICAgICAgICAgICAgICAgIGZpbGwgOj0gaW5wdXRfc2VsZWN0KGNob2ljZXMgPSBjKFwicmVkXCIsIFwiZ3JlZW5cIiwgXCJibHVlXCIsIFwieWVsbG93XCIpLCBcbiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBzZWxlY3RlZCA9IFwiYmx1ZVwiLCBsYWJlbCA9IFwiRmlsbCBDb2xvclwiKSkgJT4lIFxuICBhZGRfYXhpcyhcInhcIiwgdGl0bGUgPSBcIkFnZVwiKSAlPiUgIFxuICBhZGRfYXhpcyhcInlcIiwgdGl0bGUgPSBcIkJpbiBDb3VudFwiKSIsInNjdCI6ImV4KCkgJT4lIGNoZWNrX29iamVjdChcImNob2xcIilcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiZ2d2aXNcIilcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwibGF5ZXJfaGlzdG9ncmFtc1wiKSAlPiUgY2hlY2tfYXJnKCd3aWR0aCcpICU+JSBjaGVja19hcmcoJ2NlbnRlcicpICU+JSBjaGVja19hcmcoJ2ZpbGwnKVxuZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJhZGRfYXhpc1wiLCBpbmRleCA9IDEpICU+JSBjaGVja19hcmcoJ3ZpcycpICU+JSBjaGVja19hcmcoJ3RpdGxlJylcbmV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwiYWRkX2F4aXNcIiwgaW5kZXggPSAyKSAlPiUgY2hlY2tfYXJnKCd2aXMnKSAlPiUgY2hlY2tfYXJnKCd0aXRsZScpIn0=


interactive histogram 1

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 input_select(). 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 install.packages("shiny").

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 most important features of your data by creating beautiful visualizations with ggplot2 and base R graphics.

Conclusion

There are a lot of options to make histograms in R. The option you should choose is really rather a trade-off between what you want to accomplish and how fast you want to accomplish 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?”.