NGS

R语言绘制waffle chart

2021-06-29  本文已影响0人  维凡生物

R绘制waffle chart的小示例

library(waffle)
library(ggplot2)
library(dplyr)
# 创建数据集
df <- data.frame(group = LETTERS[1:5],
                 value = c(25, 16, 21, 15,23 ))

# 绘图
ggplot(df, aes(fill = group, values = value)) +
  geom_waffle(n_rows = 10, size = 0.33, colour = "white") +
  scale_fill_manual(name = NULL,
                    values = c("#BA182A", "#FF8288", "#FFDBDD","#FEB24C", "#FC4E2A"),
                    labels = c("A", "B", "C","D","E")) +
  coord_equal() +
  theme_void()
image.png
library(ggplot2) 
library(RColorBrewer) 
library(reshape2) 

#数据
nrows <- 8
categ_table <- round(table(mpg$class ) * ((nrows*nrows)/(length(mpg$class)))) 
sort_table<-sort(categ_table,index.return=TRUE,decreasing = FALSE) 
Order<-sort(as.data.frame(categ_table)$Freq,index.return=TRUE,decreasing = FALSE) 
df <- expand.grid(y = 1:nrows, x = 1:nrows) 
df$category<-factor(rep(names(sort_table),sort_table), levels=names(sort_table)) 

#颜色
Colormap<-brewer.pal(length(sort_table), "Set2") 
#绘图
ggplot(df, aes(x = y, y = x, fill = category)) +
  geom_tile(color = "white", size = 0.25) +
  geom_point(color = "black",shape=21,size=6) +
  coord_fixed(ratio = 1)+ 
  scale_x_continuous(trans = 'reverse') + 
  scale_y_continuous(trans = 'reverse') +
  scale_fill_manual(name = "Category", values = Colormap)+ 
  theme(panel.background = element_blank(), plot.title = element_text(size = rel(1.2)), legend.position = "right")
image.png
上一篇 下一篇

猜你喜欢

热点阅读