本文介绍了试图用ggplot2填充饼图中的大缺口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图用ggplot2创建饼图,但似乎无法在线使用其他参考。我创建的图表缺少大部分填充。

  ggplot(sae,aes(x = 1,fill = factor(State ),width = 1))+ 
geom_bar()+
ggtitle(House by State)+
coord_polar(theta ='y')




Attempting to create pie chart with ggplot2 but cannot seem to get it using other references online. The chart I create is missing most of its fill.

ggplot(sae,aes(x=1,fill=factor(State), width=1))+        
geom_bar()+
ggtitle("House by State")+
coord_polar(theta='y')

This code gives:

How do I fill the center?

Any other improvements appreciated.

解决方案

With sample data

sae <- data.frame(State=sample(LETTERS[1:6],60,T))


ggplot(sae,aes(x=factor(1),fill=factor(State)))+        
  geom_bar(width=1)+
  ggtitle("House by State")+
  coord_polar(theta="y")

EDIT: Other options (because piecharts are bad)

#following Jaaps example: some better way to visualize this
#grouped barchart

p1 <- ggplot(sae, aes(x=State, fill=State)) +
  geom_bar() + labs(title="grouped barchart")


#stacked barchart; especially practical if you want to compare groups
sae$group <- rbinom(60,1,0.5)
p2 <- ggplot(sae, aes(x=factor(group),fill=State))+
  geom_bar(width=0.5) + labs(title="grouped stacked barchart")

do.call(grid.arrange,list(grobs=list(p1,p2),ncol=2))

这篇关于试图用ggplot2填充饼图中的大缺口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 10:48