Home > Enterprise >  Show values and sample size in balloon plot in R - ggballoonplot
Show values and sample size in balloon plot in R - ggballoonplot

Time:07-30

given the data

mockdata <- data.frame(matrix(c(1,0.5,6,1,0.8,7,0.8,1,6,1,1,8),
                              ncol=3, byrow=TRUE))

colnames(mockdata) <- c("percentage_a","percentage_b", "sample_size")

rownames(mockdata) <- c("A","B","C","D")

I wanted to have each balloon coloured by the value and sized by the column sample_size.

with the following code

library(ggpubr)

ggballoonplot(mockdata,  fill = "value") 
  geom_text(aes(label=value), alpha=1.0, size=3, nudge_x = 0.15)*

I get the following graph:

graph

I can't seem to manage to use the sample_size as size of the respective column.

Any suggestion?

CodePudding user response:

I don't think ggballoonplot is the right tool for data in this format. In any case, this function is just a thin wrapper around some fairly basic ggplot code, and it is straightforward to make the plot you want directly. This gives you full control over all the plot elements.

The key is to convert your row names to a column, then pivot the two percentage columns into long format to get the data in the right format for plotting.

library(tidyverse)

mockdata %>%
  rownames_to_column() %>%
  pivot_longer(starts_with("percentage")) %>%
  ggplot(aes(name, rowname, fill = value, size = sample_size))  
  geom_point(shape = 21)  
  scale_size_continuous(limits = c(4, 8), range = c(2, 8))  
  labs(x = NULL, y = NULL)  
  theme_minimal(base_size = 16)

enter image description here

Created on 2022-07-29 by the reprex package (v2.0.1)

  • Related