我试图在没有显示0,0%值的情况下进行coord_polar绘图。我试过这个但是我有一个错误(错误:美学必须是长度1或与数据(2)相同:x,y,标签,填充)
a <- c("A", "B", "C")
b <- c(0, 20, 40)
c<- data.frame(a,b)
c$pct <- c$b/sum(c$b) #labels positions
p_c <- ggplot(c, aes(x=0.5, y=b, fill=a)) +
geom_bar(stat="identity", color="white", width=1) +
coord_polar(theta='y') +
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank())+
geom_text(data=subset(c,b >0.05),aes(x=1.2, y=cumsum(c$b) - c$b/2,
label=percent(c$pct)))+
labs(x=NULL, y=NULL)
p_c
干杯
答案 0 :(得分:0)
不要subset
您的数据。使用ifelse
:
ggplot(c, aes(x=0.5, y=b, fill=a)) +
geom_bar(stat="identity", color="white", width=1) +
coord_polar(theta='y') +
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x=element_blank())+
geom_text(data=c,aes(x=1.2, y=cumsum(c$b) - c$b/2,
label=ifelse(b>0.5,percent(c$pct),"")))+
labs(x=NULL, y=NULL)