簡體   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