1

I want to vary the size of points created with stat_summary_2d, at present they are all the same size which isn't informative. I would normally use geom_point() but because I want the points central to their correct bins it isn't appropriate, they are inconsistent in their placement relative to the bin centres so an offset also doesn't work.

Example figure, I'd like to vary the size of the points based on a n_distinct(event_key)

The code relevant for creating the points is:

  stat_summary_2d(data = filter(ni_eff, fishyear %in% c('2013', '2014', '2015', '2016') & coverage),
                  mapping  = aes(x = start_long, y = start_lat, z=event_key), 
                  binwidth = c(1, 1),
                  geom = 'point',
                  colour = 'red',
                  shape = 1,
                  # size = event_key,
                  fun     = n_distinct) +

Stat_summary_2d doesn't like size in the aesthetics, and also doesn't recognise event_key if it is entered to determine size. I think it should be possible but haven't worked out how, so any help would be appreciated.

Ultimately, I want points centred on 1*1 degree lat-long bins that vary in size given a column event_key within the dataframe with their co-ordinates.

Marine-Max
  • 324
  • 1
  • 2
  • 9

2 Answers2

2

Similar question with an applicable answer:

Map with geom_bin2d overlay with additional stat info

Applying my code:

point_df <- ggplot(filter(ni_eff, fishyear %in% c('2013', '2014', '2015', '2016') & coverage),
     aes(x = start_long, y = start_lat)) +
     stat_summary_2d(aes(z = event_key), binwidth = c(1, 1), fun = length)
df <- ggplot_build(point_df)$data[[1]]

And the size varying points are plotted using geom_point() and at the correct lat-long bin centres (x, y) generated from ggplot_build()

geom_point(data = df, aes(x = x, y = y, size = value),
                   colour = "red", shape = 1)

enter image description here

Community
  • 1
  • 1
Marine-Max
  • 324
  • 1
  • 2
  • 9
0

You could add the points manually without stat_summary2d() and then use position_nudge to adjust the position so the dots are centered. For example:

df = data.frame(a = 1:10, b = 1:10, c = 1:10)
d <- ggplot(df, aes(a, b, z = c))
d + stat_summary_2d() +
  geom_point(aes(x = a, y = b, size = c), 
             position = position_nudge(-.1, -.1))
Vandenman
  • 3,046
  • 20
  • 33