1

The facet_grid() function in is very useful for visualizing multiple dependencies between variables. However sometimes, there is a limit to which it still makes sense to visualize the data as, for example, and it may be better to just use a table.

Let´s assume I have visualized some data like this:

library(dplyr)
library(ggplot2)

set.seed(123)

cat1 <- as.character(sample(1:7, 1000, replace = T))
cat2 <- as.character(sample(1:9, 1000, replace = T))
cat3 <- as.character(sample(1:3, 1000, replace = T))
count <- sample(1:1000, 100, replace = T)
df <- data.frame(cat1, cat2, cat3, count)

df <- df %>%
  group_by(cat1, cat2, cat3) %>%
  summarise(count = sum(count)) %>%
  mutate(share = count / sum(count) * 100)

ggplot(df, aes(cat1, share)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = paste0(round(df$share, 2), "\n(", df$count, ")")), size = 3) +
  facet_grid(cat3 ~ cat2, scales = "free")

Now I have decided, that it makes more sense to just use a table. Is there a way to transform the visualization structure in this example into a table that uses the same category dependencies and contains the shares and counts in its cells?

Thanks for your suggestion.

massisenergy
  • 1,764
  • 3
  • 14
  • 25
Lutz
  • 223
  • 5
  • 15

1 Answers1

0

Hope I get you correct, I guess you can use geom_table() in ggpmisc, first you need to nest the table inside the facet categories:

library(ggpmisc)
library(tidyr)
library(dplyr)
library(ggplot2)

df %>% 
mutate(share=round(share,digits=3)) %>% 
nest(data=c(cat1,count,share))
# A tibble: 27 x 3
# Groups:   cat2 [9]
   cat2  cat3            data
   <fct> <fct> <list<df[,3]>>
 1 1     1            [7 × 3]
 2 1     2            [7 × 3]
 3 1     3            [7 × 3]
 4 2     1            [7 × 3]
 5 2     2            [7 × 3]
 6 2     3            [7 × 3]
 7 3     1            [7 × 3]
 8 3     2            [7 × 3]
 9 3     3            [7 × 3]
10 4     1            [7 × 3]

And we can plot it:

df %>% 
mutate(share=round(share,digits=3)) %>% 
nest(data=c(cat1,count,share)) %>% 
ggplot() + geom_point(aes(x=0,y=0),color=NA) +
geom_table(aes(x=0,y=0,label = data),
vjust="middle",hjust="middle",size=2)+
facet_grid(cat3 ~ cat2) + theme_void()

enter image description here

You can also check out this discussion and also the manual for ggpmisc.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72