Search code examples
rggplot2facet-wrap

How to add labels to facet_wrap(ed) geom_count plot?


I am creating a facetted plot using facet_wrap. I want text labels to be included inside the bubble. Instead it seems the total is included as label - i.e. all graphs has the same numbers but different bubble size (which is correct).

(Edits)

My code:

Category1 <- c('A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B','C','A','B')
Category2 <- c('W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V','W','V')
Class <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)

df <- data.frame(Category1, Category2, Class)


g <- ggplot(df, aes(Category1, Category2))

g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) + scale_size_continuous(range = c(5, 10))
labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")

g

g2 <-  g + geom_text(data=ggplot_build(g)$data[[1]], aes(x, y, label=n), size=2) #+ scale_size(range = c(5, 15))
g2

I expect that the size of the bubble will be indicated by the text inside the bubble. But the actual result is all graphs have the same number. I want the small bubble to have small number proportional to its size.

enter image description here


Solution

  • The problem is that your code using ggplot_build data does not have the same categories as the original. You need to create a count data before hand and use it for plotting.

    Create count data

    library(tidyverse)
    df_count <- df %>%
        count(Class, Category1, Category2)
    

    Plot

    There are two ways to incorporate this new data.

    Method 1

    The first example I show is to use both df and df_count. This method will modify your code minimally:

    g <- ggplot(df, aes(Category1, Category2))
    
    g <- g + facet_wrap(Class ~ ., nrow = 3) + geom_count(col="tomato3", show.legend=F) + 
        geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) + 
        scale_size_continuous(range = c(5, 10)) + 
        labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
    g
    

    The line geom_text(data = df_count, aes(Category1, Category2, label=n), size=2) + is added.

    Method 2

    This method uses only the count data. It uses geom_point() instead of geom_count() and alter the size using the variable n. This method is probably better in terms of code readability.

    g_alternative <- ggplot(df_count, aes(Category1, Category2,  label = n)) + 
        facet_wrap(Class ~ ., nrow = 3) + 
        geom_point(col="tomato3", aes(size = n),  show.legend=F) + 
        geom_text() + 
        scale_size_continuous(range = c(5, 10)) + 
        labs(subtitle="Count Plot", y="Category2", x="Category1", title="Cat1 vs Cat2")
    g_alternative
    

    The output looks like this:

    enter image description here