how to use geom_count() and print count labels on plot points ?

Hello,
I would like to print counts on the plot points utilizing the fact that geom_count() does the counting.
Can you tell what should be changed in geom_text() line to get this done? What to put in the label = ... command?

(the plot uses Galton's parent & child height data)

library(tidyverse)
library(UsingR)
ggplot(galton, aes(parent, child))+
  geom_count()+
  geom_smooth(method = "lm")+
  geom_text(label = ..., color="red")

Does the answer to this StackOverflow thread get you there?

In fact, I have come up with a similar solution without geom_count() - thus it is not so quick or elegant as it potentially could be.
And geom_smooth() does the regression on the aggregated data instead of raw.
Therefore using geom_count() seems better, if possible here..

EDIT: I found a way to show proper regression with geom_smooth() by making a new data frame with original galton and aggregated df

STILL the question remains - is there a quicker way to do this with geom_count()?


library(tidyverse)
library(UsingR)
pc <- xyTable(galton$parent, galton$child)
pc <- as.data.frame(pc)
names(pc) <- c("parent","child","number")

df <- merge(galton,pc)

ggplot(df)+
  aes(parent, child, size=number)+
  geom_point()+
  geom_smooth(method="lm", show.legend = F)+
  geom_text(aes(label=number, color = "red", size=2), show.legend = F)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.