r - 如何从 geom_tile 制作 donut chart

标签 r ggplot2 donut-chart

我想根据分类数据制作一个简单的圆形 donut 注释图。所以,我先做一个简单的geom_tile绘图,然后添加 coord_polar()功能。但是,我似乎无法控制ylim来制作 donut chart ,因为它会产生以下错误:

Error: Discrete value supplied to continuous scale.


现在我被这个数字困住了:
See current plot
重现情节,并帮助我:
examp = data.frame(rep(c("A","B"),each=12),
               rep(c("C","D"),times=12),
               rep(c("E","F","G"),each=8),
               rep(c("E","H","G"),each=8),
               1:24)
colnames(examp) = c("col1", "col2", "col3","col4","person")
examp$person = paste0("per.",examp$person)
examp2 <- melt(examp, id.var = 'person')

ggplot(examp2, aes(x=person,y=variable)) +
  geom_tile(aes(fill = value), colour = "white")+ 
  scale_y_discrete(breaks=y_breaks, labels=y_labels) +
  coord_polar()+
  theme(panel.background=element_blank(),
        axis.title=element_blank(),
        panel.grid=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.text.y=element_text(size=5))

最佳答案

是否在scale_y_discrete()内指定扩展量为你工作?

ggplot(examp2, aes(x=person, y=variable)) +
  geom_tile(aes(fill = value), colour = "white")+ 
  scale_y_discrete(expand = expansion(mult = c(0.5, 0), add = 0)) + # change amount if needed
  coord_polar()+
  theme(panel.background=element_blank(),
        axis.title=element_blank(),
        panel.grid=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.text.y=element_text(size=5))
(我跳过了中断/标签,因为您实际上并没有在问题中定义它们。)
enter image description here

关于r - 如何从 geom_tile 制作 donut chart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63658313/

相关文章:

r - Quanteda 中的 tokens_compound() 改变了特征的顺序

r - ggplotly 与水平 geom_crossbar() 不匹配原始 ggplot

r - 如何从 geom_密度/stat_密度 显示 AUC 值

r - 如何在 ggplotly 中为条形图更改悬停背景颜色

python - 如何围绕散点图上的一个点绘制圆环图?

javascript - 如何在 D3 中为饼图添加颜色标签?

r - ggplot 圆环图百分比标签

r - 带有滞后的 ID 累积总和

r - 如何在R中的全局环境中编写函数的结果

r - mapply 的 MoreArgs 参数什么时候不能被 R 的向量回收规则替换?