
- ggplot2 教程
- ggplot2 - 主頁
- ggplot2 - 導言
- ggplot2 - R 安裝
- ggplot2 - 在 R 中的預設繪圖
- ggplot2 - 使用座標軸
- ggplot2 - 使用圖例
- ggplot2 - 散點圖和抖動圖
- ggplot2 - 條形圖和直方圖
- ggplot2 - 餅狀圖
- ggplot2 - 邊緣圖
- ggplot2 - 氣泡圖和計數圖
- ggplot2 - 發散圖
- ggplot2 - 主題
- ggplot2 - 多面板圖
- ggplot2 - 多重圖
- ggplot2 - 背景色
- ggplot2 - 時間序列
- ggplot2 有用的資源
- ggplot2 - 快速指南
- ggplot2 - 有用的資源
- ggplot2 - 討論
ggplot2 - 餅狀圖
餅狀圖被認為是一種圓形統計圖,它被劃分為多個扇形來顯示數值比例。在提到的餅狀圖中,每個扇形的弧長與其表示的數量成正比。弧長表示餅狀圖的角度。餅狀圖的總度數為 360 度。半圓或半餅狀圖包括 180 度。
建立餅狀圖
在提到的工作空間中載入軟體包,如下所示 -
> # Load modules > library(ggplot2) > > # Source: Frequency table > df <- as.data.frame(table(mpg$class)) > colnames(df) <- c("class", "freq")

可以使用以下命令建立樣本圖表 -
> pie <- ggplot(df, aes(x = "", y=freq, fill = factor(class))) + + geom_bar(width = 1, stat = "identity") + + theme(axis.line = element_blank(), + plot.title = element_text(hjust=0.5)) + + labs(fill="class", + x=NULL, + y=NULL, + title="Pie Chart of class", + caption="Source: mpg") > pie
如果您觀察輸出,該圖表不會以圓形方式建立,如下所示 -

建立座標
讓我們執行以下命令來建立所需的餅狀圖,如下所示 -
> pie + coord_polar(theta = "y", start=0)

廣告