• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to write a function make histograms with ggplot

datavizpyr · November 25, 2022 ·

In this tutorial, we will learn how to write a function to make a histograms using ggplot2 in R. Writing a function to create plots is useful, when you make a lot of plots, in this example, a lot of histograms.

Thanks to the upcoming 2nd edition of R for Data Science by Hadley Wickham, we have simple examples to write functions under a variety of scenarios.

Let us load tidyverse and palmerpenguin packages to make histogram

library(tidyverse)

Without using a function, our code will look this to make a simple histogram.

penguins %>%
  ggplot(aes(x=flipper_length_mm))+
  geom_histogram(color="white")+
  theme_bw(16)
ggplot2 histogram example
ggplot2 histogram example

Plotting function for making histograms

If we have to make many histograms, we have to repeat ourselves many times. And writing a function will help us prevent repeating ourselves and thus reducing opportunities to make any mistakes.

# a simple function to make histogram 
histogram <- function(df, variable){
  df %>%
    ggplot(aes({{ variable }}))+
    geom_histogram(color="white", fill="dodgerblue")+
    theme_bw(16)
}

We have basically wrapped out code to make a histogram into a function. One big thing to notice is how we refer the variable inside the function. We use {{ }} to refer the variable inside aes() function.

Now we can us the function to make histogram of variable of interest. Here is an example.

histogram(penguins, body_mass_g)
ggsave("ggplot_histogram_function_example1.png")

Writing a function to make plot: Example 1
Writing a function to make plot: Example 1

Here is another example of histogram function call with another numerical variable.

histogram(penguins, flipper_length_mm)
ggsave("ggplot_histogram_function_example2.png")

Writing a function to make plot: Example 2
Writing a function to make plot: Example 2

Customizing plot function for making histograms

The function wee wrote to make histogram is pretty basic, we can further customise the plotting function in numerous ways. Here is one example of customising the function, where we use rlang’s eenglue() function to create a title that shows the variable name used in the histogram.

histogram <- function(df, variable){
  # text for title of the plot
  title_text <- rlang::englue("Histogram of {{variable}}")
  df %>%
    ggplot(aes({{ variable }}))+
    geom_histogram(color="white", fill="dodgerblue")+
    theme_bw(16)+
    labs(title=title_text)
}

And if we use the updated histogram with function, we will get plot with the title.

histogram(penguins, body_mass_g)
Customising Histogram plotting function
Customising Histogram plotting function

Related posts:

How to add colors to axis tick labels using ggtext?How to Add Colors to Axis Tick Label in ggplot2 Customizing lollipop. plot with geom_lollipop() in ggaltLollipop plot with ggplot extension ggalt How to Increase Color Legend's Point Size in ggplot2How to Increase Legend Key Size in ggplot2 How to reverse legend key order in ggplot2How to reverse legend key order in ggplot2

Filed Under: ggplot2, R, Uncategorized Tagged With: plot function ggplot2

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version