2

I really like how balloonplot generates the balloon plots with the grid, moreover it keeps the cells with value of zero but does not show the circle. Here is an example,

dt <- as.table(as.matrix(mtcars[1:10,]))
balloonplot(t(dt), xlab ="", ylab="", label = FALSE, show.margins = FALSE, cum.margins=F)

When I tried to generate this in ggplot is a mess, because I can't get the grid to show up correctly and moreover the cells with zero are showing up as dot and not blank.

ggplot(melt(dt), aes(x =X2, y = X1))  +geom_point( aes(size=value),shape=21, colour="black", fill="skyblue")

Is there a way to plot this with ggplot OR alternatively save the gplot as a ggplot object? The reason why I'm I need a ggplot object is because I plan to sticth this as a bigger overall figure with pathwork.

# this fails 
as.ggplot(balloonplot(t(dt), xlab ="", ylab="", label = FALSE, show.margins = FALSE))

thanks!

enter image description here

Ahdee
  • 4,679
  • 4
  • 34
  • 58

2 Answers2

2

The closest I could get was the ggpubr balloon plot - does this suit your needs?

library(tidyverse)
#install.packages("ggpubr")
library(ggpubr)

mtcars[1:10,] %>%
ggballoonplot(size.range = c(-0.5,10), rotate.x.text = FALSE,
              ggtheme = theme_minimal(base_size = 14)) +
  ggtitle(label = "Balloon Plot for x by y",
          subtitle = "Area is proportional to Frequency") +
  scale_x_discrete(position='top') +
  theme(panel.grid.major = element_blank(),
        axis.text.x = element_text(size = 14),
        plot.title.position = "plot",
        plot.title = element_text(face = "bold"),
        plot.subtitle = element_text(face = "bold"),
        axis.ticks = element_blank()) +
  geom_tile(color = "black", fill = "transparent")

example_balloon_plot.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
1

How about this?

library(tidyverse)
dt2 <- as.data.frame(as.matrix(mtcars[1:10,]))

dt2 %>%
  rownames_to_column("Car") %>%
  gather(Variable, Value, -Car) %>%
  ggplot(aes(x = Variable, y = Car, size = Value)) +
  geom_point(colour = "sky blue") +
  scale_size_continuous(range = c(-1, 10)) +
  labs(title = "Balloon Plot for x by y",
     subtitle = "Area is proportional to Freq.",
     x = "",
     y = "") +
  theme_bw()

enter image description here

mj_whales
  • 124
  • 2
  • 11
  • thanks, I had something similar as well, where I plot it in two layers, the first setting alpha to 0 however the same issued occur which that the grid lines are crossing the circles and not surrounding the circles as with balloonplot. – Ahdee Sep 29 '20 at 03:59
  • @Ahdee I see what you mean. Well to make your zero cells blank instead of zero, simply make the lower end of the size scale negative, like this: scale_size_continuous(range = c(-1, 10)). That should fix one of your issues. – mj_whales Sep 30 '20 at 03:38