0

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

4
  • The plot in your post is not the output of the code you provided. Commented Apr 5, 2019 at 8:35
  • Agreed with @J_F. Your code does not reproduce the figure. Commented Apr 5, 2019 at 8:39
  • @amatsuo_net, Pls see edits. Commented Apr 5, 2019 at 9:43
  • I posted my answer. Commented Apr 5, 2019 at 10:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.