如何使用ggplot在R中绘制变体圆形条形图 [英] How to plot the variant circular bar chart in r with ggplot

查看:325
本文介绍了如何使用ggplot在R中绘制变体圆形条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制这样的图片,但我不知道如何用R绘制.我在

I want to plot a picture like this but I don't know how to plot it with R. I saw a tutorial on this website but this is not what I needed. It is like a circular bar chart except that, in the picture below every bar is not a single value but a series value denote different incidence in a different year.

下图中是模板.

任何帮助将不胜感激!

我的虚假数据:

structure(list(year = c(2010, 2011, 2012, 2010, 2011, 2012, 2010, 
2011, 2012, 2010, 2011, 2012), disease = c(1, 1, 1, 2, 2, 2, 
3, 3, 3, 4, 4, 4), group = c("A", "A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "B"), incidence = c(0.2, 0.3, 0.4, 0.1, 0.3, 
0.2, 0.5, 0.6, 0.7, 0.8, 0.9, 0.3)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

推荐答案

这是一个循环的热图.下面就像是ggplot2中的一个快速尝试:

It is a circular heatmap.. most likely there are packages for doing it. Below is like a quick attempt in ggplot2 to do it:

library(ggplot2)
library(tidyr)
library(dplyr)
library(tibble)

mat = matrix(runif(200),10,20)
colnames(mat) = paste0("col",1:ncol(mat))
rownames(mat) = paste0("row",1:nrow(mat))

假设mat是我们的矩阵,然后我们进行长轴旋转并将行和列顺序保留为因子:

Suppose mat is our matrix, then we pivot long and preserve the row and column orders in factor:

df = data.frame(mat) %>% 
rownames_to_column("row") %>% 
pivot_longer(-row) %>%
mutate(name=factor(name,levels=colnames(mat)),
row=factor(row,levels=rownames(mat)))

然后我们绘制,在下面添加了另一列以添加行名注释:

Then we plot, below I added another column to add the row names annotation:

row_num = length(levels(df$row))

g = ggplot(df,aes(x=name,y=as.numeric(row),fill=value)) + 
xlim(c("",colnames(mat))) + ylim(c(-row_num/1.5,row_num+1))+
geom_tile()+ ylab("")+
annotate(x="",y=1:row_num,label=levels(df$row),size=2.5,geom="text") 

因此,您有一个高架"的热图.第一列是标签.现在的目的是包装它并形成圆形热图:

So you have a "elevated" heatmap. And the first column is the label. The purpose is to now wrap this and form the circular heatmap:

g + coord_polar(start=-0.15) + theme_bw() + 
theme(legend.position = c(0.5, 0.5),legend.key.size = unit(0.2, "cm")) 

这篇关于如何使用ggplot在R中绘制变体圆形条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆