R語(yǔ)言三種方法繪制餅圖

R語(yǔ)言中繪制餅圖,有基礎(chǔ)繪圖包的pie()函數(shù),但是做出來(lái)的圖不夠美觀。ggplot2及其拓展包做出來(lái)的圖美觀。下面提供三種以ggplot2及其拓展包繪制餅圖的方法。

以mtcars數(shù)據(jù)集為例子,要查看32輛車中Cylinders數(shù)的分布情況。

一、常見(jiàn)的先繪制條圖,再用coord_polar()轉(zhuǎn)換成餅圖

數(shù)據(jù)準(zhǔn)備,第一、二種方法需要。

data("mtcars")#加載數(shù)據(jù)
table <- table(mtcars$cyl) #計(jì)算頻數(shù)
table
 4  6  8 
11  7 14 
data <- as.data.frame(table) #將頻數(shù)表轉(zhuǎn)換成數(shù)據(jù)框
data
  Var1 Freq
1    4   11
2    6    7
3    8   14
colnames(data) <- c('Cylinders', 'Freq') #修改數(shù)據(jù)框的列名
data
  Cylinders Freq
1         4   11
2         6    7
3         8   14
percentage <- scales::percent(data$Freq / sum(data$Freq)) #計(jì)算百分比,利用scales包的percent()函數(shù),將計(jì)算的小數(shù)比例轉(zhuǎn)換成百分?jǐn)?shù)
percentage
[1] "34.4%" "21.9%" "43.8%"
labs <- paste(data$Cylinders, '-Cylinders', '(', percentage, ')', sep = '')#設(shè)置標(biāo)簽名
labs
[1] "4-Cylinders(34.4%)" "6-Cylinders(21.9%)" "8-Cylinders(43.8%)"
library(ggplot2)
library(magrittr)
#繪制條圖
p1 <- data %>% 
    ggplot(aes(x = '', y = Freq, fill = Cylinders)) + 
    geom_bar(stat = 'identity', width = 1) + 
    geom_text(aes(y = cumsum(rev(Freq))-round(rev(Freq)*2/3), label = labs[seq(length(labs), 1, -1)])) + #y = ?設(shè)置標(biāo)簽所在的位置,如果不設(shè)置y = ?,y會(huì)默認(rèn)為坐標(biāo)值,所有標(biāo)簽位置會(huì)發(fā)生偏移。
    theme_bw() + 
    labs(x = '', y = '',title = 'Number of cars in different Cylinders') #清除x-y軸的標(biāo)題,設(shè)置主標(biāo)簽。
p1

Rplot02.png

不設(shè)置aes(y = ?),條圖標(biāo)簽的位置

data %>% 
  ggplot(aes(x = '', y = Freq, fill = Cylinders)) + 
  geom_bar(stat = 'identity', width = 1) + 
  geom_text(aes(label = labs)) +  #未設(shè)置y
  theme_bw() + 
  labs(x = '', y = '',title = 'Number of cars in different Cylinders') 
image.png

用coord_polar()轉(zhuǎn)換成餅圖

p2 <- p1 + coord_polar(theta = 'y', start = 0, direction = 1) #direction = 1,順時(shí)針,direction = -1, 逆時(shí)針?lè)较颉?p2
Rplot05.png

不設(shè)置aes(y = ?),餅圖標(biāo)簽的位置

data %>% 
  ggplot(aes(x = '', y = Freq, fill = Cylinders)) + 
  geom_bar(stat = 'identity', width = 1) + 
  geom_text(aes(label = labs)) + #未設(shè)置y
  theme_bw() + 
  labs(x = '', y = '',title = 'Number of cars in different Cylinders')  + 
    coord_polar(theta = 'y', start = 0, direction = 1)
不設(shè)置y的情況.png

二、利用ggpubr包的ggpie()

data$labs <- labs
library(ggpubr)
p3 <- ggpie(data, 'Freq',  #繪圖,只用寫頻數(shù)就行,切記不用再寫分組
      fill = 'Cylinders', palette = 'jco', #按照Cylinders填充,顏色板為jco.
      label = labs, lab.pos = 'in', lab.font = c(4, 'white')) #設(shè)置標(biāo)簽,標(biāo)簽的位置在圖的內(nèi)部,標(biāo)簽的大小為4, 顏色為白色.
p3
Rplot03.png

三、ggstatsplot::ggpiestats()

library(ggstatsplot)
p5 <- ggpiestats(mtcars, 'cyl', 
           results.subtitle = F, #標(biāo)題中不顯示統(tǒng)計(jì)結(jié)果
           factor.levels = c('4 Cylinders', '6 Cylinders', '8 Cylinders'),#設(shè)置標(biāo)簽的名稱
           slice.label = 'percentage', #設(shè)置餅圖中標(biāo)簽的類型(默認(rèn)percentage:百分比, 還有counts:頻數(shù), both : 頻數(shù)和百分比都顯示)
           perc.k = 2, #百分比的小數(shù)位數(shù)為2
           direction = 1, #1為順時(shí)針?lè)较颍?1為逆時(shí)針?lè)较?           palette = 'Pastel2', #設(shè)置調(diào)色板
           title = 'Number of cars in different Cylinders'#設(shè)置標(biāo)題)
p5
第三種方法:ggstatsplot::ggpiestats().png

三種方法比較:

第一種方法需要手動(dòng)設(shè)置標(biāo)簽的位置,反復(fù)地調(diào),直到合適為止,比較麻煩。

第二種方法不需要手動(dòng)設(shè)置標(biāo)簽位置,自動(dòng)在圖中展示好,比第一種方法好點(diǎn)兒。

前兩種方法需要對(duì)數(shù)據(jù)進(jìn)行頻數(shù)計(jì)算等操作做數(shù)據(jù)準(zhǔn)備工作,第三種方法直接利用原數(shù)據(jù),不需要數(shù)據(jù)準(zhǔn)備工作,方便快捷。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容