Search code examples
rggplot2summary

Summary statistics as aesthetics argument for geom_point


I would like to create a plot with dots/ points/ circles (e.g., using geom_point())

Dummy data:

tp = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L)
value = c(-0.602414496676874, -0.602414496676874, -0.602414496676874, -0.602414496676874, 
          -1.43908106523151, -1.43908106523151, -1.43908106523151, -1.43908106523151, 
          -2.19530757454171, -2.19530757454171, -1.90848346932521, -2.19530757454171, 
          -1.01688680882049, -0.26861160987711, -2.26401214039278, -2.26401214039278, 
          -2.40645987252632, -1.89777729784596, -2.40645987252632, -2.40645987252632)
x = c(0, 0, 0, 0, 1, 1, 1, 1, 0.866025403784439, 0.866025403784439, 0.866025403784439,
      0.866025403784439, 0.5, 0.5, 0.5, 0.5, 0, 0, 0, 0)
y = c(0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5, 0.866025403784439, 0.866025403784439, 
      0.866025403784439, 0.866025403784439, 1, 1, 1, 1)

dat = data.frame (tp, x, y, value)

About the data: It contains measurements at specific locations (tp) on a map, having the discrete coordinates x / y. Inherent to the data is the overlap of many measurements on those test points (tp). What I now would like to do is to map summary statistics such as mean / median / sd, etc to those coordinates.

Solution A (gives required result): I could of course create a summary data frame and pipe into my plot.

require(dplyr)
require(ggplot2)
dat %>% group_by(x, y) %>% summarise(mean = mean(value)) %>%
  ggplot(aes(x, y, color = mean)) + geom_point() 

enter image description here

But I am curious if there is any geom_... in ggplot which would allow a more direct 'stats mapping'.

For example stat_summary_2d (not really what I want): This might go in the direction, but I did not find a way to modify the shape of the resulting rectangles/ hexagons.

ggplot(dat, aes(x,y, z = value)) + stat_summary_2d(fun = 'mean') #or stat_summary_hex()

enter image description here

My question Is there any way of directly summarising variables with stat functions and use this as arguments in the aesthetics call? I would be open to any geometry that will allow to create dots/ points. Cheers

P.S. I am aware that this question might be a duplicate. If you find it, please let me know. I did not. Thanks.


Solution

  • Just specify geometry as point and change its shape to the filled one in range 21-24.

    ggplot(dat, aes(x, y, z = value))+
    stat_summary_2d(fun = 'mean', geom='point', size=5, shape=21)
    

    enter image description here