3

I used geom_count to visualise overlaying points as sized groups, but I also want to add the actual count as a label to the plotted points, like this:

enter image description here However, to achieve this, I had to create a new data frame containing the counts and use these data in geom_text as shown here:

#Creating two data frames
data <- data.frame(x = c(2, 2, 2, 2, 3, 3, 3, 3, 3, 4),
               y = c(1, 2, 2, 2, 2, 2, 3, 3, 3, 3),
               id = c("a", "b", "b", "b", "c", 
                      "c", "d", "d", "d", "e"))
data2 <- data %>% 
  group_by(id) %>%
  summarise(x = mean(x), y = mean(y), count = n())

# Creating the plot
ggplot(data = data, aes(x = x, y = y)) +
  geom_count() +
  scale_size_continuous(range = c(10, 15)) +
  geom_text(data = data2, 
            aes(x = x, y = y, label = count),
            color = "#ffffff")

Is there any way to achieve this in a more elegant way (i.e. without the need for the second data frame)? I know that you can access the count in geom_count using ..n.., yet if I try to access this in geom_text, this is not working.

4
  • 1
    why do you need data when you have data2, just plot from it.
    – pogibas
    Oct 20, 2017 at 15:00
  • 1
    @PoGibas: this is indeed an option (which, honestly, I overlooked). Nonetheless, since the geom_count is able to do the summarising calculation part for you, it would be neat if there was a way of also directly using this information
    – Bart VdW
    Oct 23, 2017 at 7:41
  • 1
    I understand, but my honest suggestion (personal experience) is to leave plotting for ggplot2 and all other calculations outside ggplot2 - data modification, cleaning, summary is done before ggplot2. Saves time and easier :-)
    – pogibas
    Oct 23, 2017 at 7:43
  • 1
    Thanks, I'll keep that in mind!
    – Bart VdW
    Oct 23, 2017 at 11:14

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

4

Are you expecting this:

ggplot(data %>% 
         group_by(id) %>%
         summarise(x = mean(x), y = mean(y), count = n()), 
       aes(x = x, y = y)) + geom_point(aes(size = count)) +
  scale_size_continuous(range = c(10, 15)) +
  geom_text(aes(label = count),
            color = "#ffffff")

enter image description here

update: If the usage of geom_count is must, then the expected output can be achieved using:

p <- ggplot(data = data, aes(x = x, y = y)) +
  geom_count() + scale_size_continuous(range = c(10, 15))
p + geom_text(data = ggplot_build(p)$data[[1]], 
              aes(x, y, label = n), color = "#ffffff")

enter image description here

2
  • Thanks, this works indeed. Nonetheless, this still involves manually calculating the information that is readily available in the geom_count. However, I still wonder if there is any way to access this information directly and save myself the summarise step (which basically geom_count does for you).
    – Bart VdW
    Oct 23, 2017 at 7:46
  • Thanks a lot! This is indeed the kind of thing I was thinking about.
    – Bart VdW
    Oct 23, 2017 at 11:13
-1

A much easier way to change this is to use the labs() function so in this case it would be ...labs(size = "Count") + ....

That should be all you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.