如何绘制图形热图但通过每个单元格中的饼图显示?

问题描述 投票:0回答:1

我想画一个包含很多饼图的图形。

通常很容易根据矩阵制作热图。

但现在我想使用更好的方法来可视化我的数据。

这是一种我认为还可以但并不完美的方法:

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

下图是这样的: enter image description here

还没有完成,下一步我会在这里添加百分比和一些其他信息,例如行间隙或列间隙。

所以在这里我希望有人给我一些建议或者告诉我更好的方法。

非常感谢。

r heatmap pie-chart
1个回答
0
投票

使用

facet_grid
将每个子集放置在自己的小多重图中:

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())

返回

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.