繁体   English   中英

ggplot2 饼图中的差距?

[英]Gaps in ggplot2 pie chart?

我想创建显示几种产品市场份额的饼图。 我想以这种方式对饼图进行分面包装,以显示产品 X 推出前后产品的市场份额.

但是,由于在 0 期该产品不存在,我的饼图没有关闭。 我尝试在第 0 期(二进制编码变量)中为该产品 X 的数据集中引入额外的行并将所有市场统计数据设置为零,希望饼图能够修复,但它不起作用,现在我有零在上面。

代码如下:

ggplot(df_graph,aes(x="", y=sales, fill=Product.Name)) +
  geom_bar(stat="identity", width = 1) +
  coord_polar(theta="y")+
  scale_fill_brewer(palette="time_period")+
  facet_wrap(~before_1345)+
  theme_void()+
  theme(legend.position = "bottom", text=element_text(family = "Times New Roman",size=12,color="black"),plot.margin = margin(c(10,1,10,1)))+
  geom_text(aes(label = paste0(round(Share_x*100, 2), "%")),family = "Times New Roman",size=4,position=position_stack(vjust=0.6),color="black",angle=45) + 
  labs(fill = "Product")

我假设饼图没有被填充,因为笛卡尔坐标中的条形图没有相同的高度。

使用内置数据集粗略地重现该问题,我们可以看到,当我们在笛卡尔坐标中有高度不等的条时......

library(ggplot2)

ggplot(mpg, aes(x = "", y = displ, fill = class)) +
  geom_col() +
  facet_wrap(~ year)

...使用极坐标时不要填充饼图:

ggplot(mpg, aes(x = "", y = displ, fill = class)) +
  geom_col() +
  coord_polar(theta = "y") +
  facet_wrap(~ year)

这很容易通过在条形层中设置position = "fill"来解决。

ggplot(mpg, aes(x = "", y = displ, fill = class)) +
  geom_col(position = "fill") +
  coord_polar(theta = "y") +
  facet_wrap(~ year)

代表 package (v2.0.1) 于 2022 年 8 月 17 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM