Search notes:

R data visualization: bar chart for mean (average) of bins/groups

The following example first creates two vectors: x and y.
Using cut, three groups are created. The first group is for x between 10 and 20, the second group for x between 20 and 30 and the third group for x between 30 and forty. Each element in bins stores to which group each element in x belongs.
bins is then used in tapply to calculate the mean for each group's y elements. The result is stored in mean_.
barplot creates a bar chart whose bars' heights correspond to the mean of each group.
Lastly, points overlays a scatter plot with the original x/y pairs.
X11()

x <- c(  13.1 , 16.2 , 12.9    ,   28.7 , 25.4   ,   31.5 , 36.5 , 33.3  ,
         11.8 , 17.6           ,   26.4 , 21.3   ,   38.3 , 37.4 , 31.5   )

y <- c(    1  ,    3 ,    6    ,      5 ,    9   ,     12 ,   10 ,   11  ,
           5  ,    8           ,     11 ,    7   ,      9 ,    8 ,    9   )

stopifnot(length(x) == length(y))

bins  <- cut(x, c(10, 20, 30, 40))
mean_ <- tapply(y, bins, mean)

barplot(mean_,
        width  = 10,
        space  =  0,
#       offset =  0,
        ylim   = c(0, max(y)+0.5),
#       xlim   = c(-10, 30)
)

points(x-10, y,
   bg='orange',
   col='red',
   pch=21,
   cex=3
)

invisible(locator(1))
Github repository about-r, path: /graphics/data-visualization/bar-chart/mean-of-bins.R

See also

Data visualization with R
Bar chart
R package oce: binAverage

Index