目錄:
- 原始圖樣
- 如何去除餅圖中心的雜點
- 如何去除餅圖旁邊的標簽
- 如何去掉左上角多出來的一橫線
- 如何去掉圖例的標題,並將圖例放到上面
- 如何對圖例的標簽加上百分比
- 如何讓餅圖的小塊按順時針從大到小的順序顯示
- 如何去掉白色外框上的數字
- 如何在圖中加百分比
- 如何生成餅環
(更多內容請見:R、ggplot2、shiny 匯總)
原始圖樣:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity") + coord_polar(theta = "y") ## 把柱狀圖折疊成餅圖(極坐標) p
如何去除餅圖中心的雜點:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + ## width >= 1 時中心的雜點將消失 coord_polar(theta = "y") p
如何去除餅圖旁邊的標簽:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") ## 將標簽設為空 p
如何去掉左上角多出來的一橫線:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) ## 把左上角多出來的“小胡子”去掉 p
如何去掉圖例的標題,並將圖例放到上面:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) + theme(legend.title = element_blank(), legend.position = "top") ## 將圖例標題設為空,並把土方放在上方 p
如何對圖例的標簽加上百分比:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) myLabel = as.vector(dt$B) ## 轉成向量,否則圖例的標簽可能與實際順序不一致 myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%) ", sep = "") ## 用 round() 對結果保留兩位小數 p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) + theme(legend.title = element_blank(), legend.position = "top") + scale_fill_discrete(breaks = dt$B, labels = myLabel) ## 將原來的圖例標簽換成現在的myLabel p
如何讓餅圖的小塊按順時針從大到小的順序顯示:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) dt = dt[order(dt$A, decreasing = TRUE),] ## 用 order() 讓數據框的數據按 A 列數據從大到小排序 myLabel = as.vector(dt$B) myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%) ", sep = "") p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) + theme(legend.title = element_blank(), legend.position = "top") + scale_fill_discrete(breaks = dt$B, labels = myLabel) p
如何去掉白色外框上的數字:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) dt = dt[order(dt$A, decreasing = TRUE),] ## 用 order() 讓數據框的數據按 A 列數據從大到小排序 myLabel = as.vector(dt$B) myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%) ", sep = "") p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) + theme(legend.title = element_blank(), legend.position = "top") + scale_fill_discrete(breaks = dt$B, labels = myLabel) + theme(axis.text.x = element_blank()) ## 白色的外框即是原柱狀圖的X軸,把X軸的刻度文字去掉即可 p
如何在圖中加百分比:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) dt = dt[order(dt$A, decreasing = TRUE),] myLabel = as.vector(dt$B) myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%)", sep = "") p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) + theme(legend.title = element_blank(), legend.position = "top") + scale_fill_discrete(breaks = dt$B, labels = myLabel) + theme(axis.text.x = element_blank()) + geom_text(aes(y = A/2 + c(0, cumsum(A)[-length(A)]), x = sum(A)/20, label = myLabel), size = 5) ## 在圖中加上百分比:x 調節標簽到圓心的距離, y 調節標簽的左右位置 p
如何生成餅環:
library(ggplot2) dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E')) dt = dt[order(dt$A, decreasing = TRUE),] myLabel = as.vector(dt$B) myLabel = paste(myLabel, "(", round(dt$A / sum(dt$A) * 100, 2), "%)", sep = "") p = ggplot(dt, aes(x = "", y = A, fill = B)) + geom_bar(stat = "identity", width = 0.3) + ## 當width < 1 時餅圖將變成餅環 coord_polar(theta = "y") + theme_bw() + labs(x = "", y = "", title = "") + theme(axis.ticks = element_blank()) + theme(legend.position = "none") + theme(axis.text.x = element_blank()) + geom_text(aes(y = A/2 + c(0, cumsum(A)[-length(A)]), x = sum(A)/24, label = myLabel), size = 5) p
版權聲明:轉載請注明出處,謝謝!