rheatmappie-chart

How to draw a figure a heatmap but shows through pie chart in each cell?


I want to draw a figure which contains many pie charts.

And usually it is easy to make a heatmap figure based on a matrix.

But now I want to use a better way to visualize my data.

Here is one method that I think is ok but not perfect:

library(ggplot2)
library(gridExtra)  # For arranging plots
#library(ChIPseeker)
library(dplyr)
library(plyr )
#??dlply
# Simulate some data
set.seed(123)
data <- expand.grid(X = factor(1:20), Y = factor(1:18))
data$Category <- rep(c("A", "B", "C"), each = 120)
data$Value <- runif(360)

# View the data
print(data)


# Function to plot a pie chart for each group
plot_pie <- function(data_subset) {
  ggplot(data_subset, aes(x = "", y = Value, fill = Category)) +
    geom_bar(width = 1, stat = "identity") +
    coord_polar(theta = "y") +
    theme_void()+
    theme(legend.position = "none")
    #labs(title = paste("X:", unique(data_subset$X), "Y:", unique(data_subset$Y)))
}

# Split data by X and Y, create a list of plots
plots <- dlply(data, .(X, Y), plot_pie)

# Arrange plots in a grid
grid.arrange(grobs = plots, ncol = 20)




###############
library(patchwork)

# Combine plots with patchwork
plot_layout <- wrap_plots(plots, ncol = 20)
plot_layout

And the figure like this: enter image description here

It is not completed,the next step I will add percentage here and some other information, sucha as row gaps or col gaps.

So here I hope somebody give me some advice or tell me better methods to do it.

Thanks a lot.


Solution

  • Using facet_grid to place each subset in its own small multiple plot:

    ggplot(data, aes(x = "", y = Value, fill = Category)) +
      geom_bar(width = 1, stat = "identity") +
      coord_polar(theta = "y") +
      facet_grid(X~Y) +
      theme_void() +
      theme(strip.text = element_blank())
    

    which returns

    enter image description here