ggplot 2--餅状図
11972 ワード
原文出典:http://blog.csdn.net/bone_ace/article/details/47455363
元の図面:
円グラフの中心の雑点を除去する方法:
円グラフの横のラベルを削除する方法:
左上の横線を取り除くにはどうすればいいですか.
凡例のタイトルを削除し、凡例を上に配置する方法:
凡例のラベルにパーセントを付ける方法:
円グラフの小さな塊を時計回りに大きい順から小さい順に表示する方法:
白い外枠の数字を削除する方法:
図にパーセントを追加する方法:
円盤リングの生成方法:
円環以外の枠と真ん中の座標線を取り除く方法:
円環のもう一つの画法(geom_rect():
元の図面:
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.2) + ## 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.5, label = myLabel), size = 5)
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.2) +
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.5, label = myLabel), size = 5) +
theme(panel.grid=element_blank()) + ##
theme(panel.border=element_blank()) ##
p
円環のもう一つの画法(geom_rect():
library(ggplot2)
ad = data.frame(type = c("Poster", "Billboard", "Bus", "Digital"),n = c(529, 356, 59, 81))
ad$fraction = ad$n / sum(ad$n)
ad$ymax = cumsum(ad$fraction)
ad$ymin = c(0, head(ad$ymax, n = -1))
ggplot(data = ad, aes(fill = type, ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +
geom_rect(colour = "grey30", show_guide = FALSE) +
coord_polar(theta = "y") +
labs(x = "", y = "", title = "") +
xlim(c(0, 4)) +
theme_bw() +
theme(panel.grid=element_blank()) + ##
theme(axis.text=element_blank()) + ##
theme(axis.ticks=element_blank()) + ##
theme(panel.border=element_blank()) + ##
geom_text(aes(x = 3.5, y = ((ymin+ymax)/2), label = type))