stat_summary for Statistical Summary in ggplot2 R

stat_summary is a unique statistical function and allows a lot of flexibility in terms of specifying the summary. Using this, you can add a variety of summary on your plots. For example, in a bar chart, you can plot the bars based on a summary statistic such as mean or median. Similarly, stat_summary() can be used to add mean/median points to a dot plot.

stat_summary() takes a few different arguments.

  • fun.y: A function to produce y aesthetics
  • fun.ymax: A function to produce ymax aesthetics
  • fun.ymin: A function to produce ymin aesthetics
  • fun.data: A function to produce a named vector of aesthetics

We can pass a function to each of these arguments, and ggplot2 will use the value returned by that function for the corresponding aesthetic. If you pass a function to fun.data, you can compute many summary statistics and return them as a vector, where each element in the vector is named for the aesthetic it should be used for.

Let's understand this with two examples:

Bar Chart with Median Values

We will use the stock_prices.tidy dataframe we created earlier to plot a bar chart with the stock symbols on the x-axis and the median stock price for each stock on y-axis. We can achieve this using the stat_summary() function as follows:

ggplot(stock_prices.tidy,aes(x=Symbol,y=Prices,fill=Symbol))+
  stat_summary(fun.y = median, geom = "bar")

Quartile Points

Following is another example where we plot quartile points for each stock. We first create a new function to calculate the quartile and then supply that function as argument to fun.data in stat_summary().

median.quartile <- function(x){
  out <- quantile(x, probs = c(0.25,0.5,0.75))
  names(out) <- c("ymin","y","ymax")
  return(out)
}
ggplot(stock_prices.tidy, aes(x=Symbol,y=Prices,col=Symbol)) +
  stat_summary(fun.data = median.quartile, geom = "pointrange")


Course Downloads

Get smart about tech at work.

As a non-technical professional, learn how software works with simple explanations of tech concepts. Learn more...

Data Science for Finance Bundle: 43% OFF

Get our Data Science for Finance Bundle for just $29 $51.
Get it now for just $29

Checkout our eBooks and Templates

eBooks and templates related to finance, R programming, Python, and Excel.
Visit Store
Get our Data Science for Finance Bundle for just $29 $51. That's 43% OFF.
Get it for $51 $29