install.packages("ggplot2") install.packages("ggrepel")load
library("ggplot2") library("ggrepel")
a=c("A1","A2","A3","A4","A5","MA","NA");a=factor(a) set.seed(0) df = rbind(data.frame(a = a[runif(10, min = 1, max = length(a)) %>% as.integer()], n=rep(1, 10)), data.frame(a = a, n = rep(0, length(a)))) df df = aggregate(df$n, by = list(a = df$a), FUN = sum); names(df)[2] = "n" df
> a=c("A1","A2","A3","A4","A5","MA","NA");a=factor(a) > set.seed(0) > df = rbind(data.frame(a = a[runif(10, min = 1, max = length(a)) %>% as.integer()], n=rep(1, 10)), data.frame(a = a, n = rep(0, length(a)))) > df a n 1 MA 1 2 A2 1 3 A3 1 4 A4 1 5 MA 1 6 A2 1 7 MA 1 8 MA 1 9 A4 1 10 A4 1 11 A1 0 12 A2 0 13 A3 0 14 A4 0 15 A5 0 16 MA 0 17 NA 0 > df = aggregate(df$n, by = list(a = df$a), FUN = sum); names(df)[2] = "n" > df a n 1 A1 0 2 A2 2 3 A3 1 4 A4 3 5 A5 0 6 MA 4 7 NA 0runif() による一様乱数で A1〜A5、複数回答(MA)、無回答(NA) から 10 個適当に拾って、
y | weight | |
---|---|---|
stack | geom_col() | geom_bar() |
fill | geom_col(position="fill") | geom_bar(position="fill") |
ggplot(df) + aes(x="", y=n, fill=a) + geom_col()
ggplot(df) + aes(x="", y=n, fill=a) + geom_col(position="fill")
ggplot(df) + aes(x="", y=n, fill=a) + geom_col() + geom_label(aes(y=ifelse(0<n,n,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),position=position_stack(vjust=0.5),na.rm=T,show.legend=F)
ggplot(df) + aes(x="", y=n, fill=a) + geom_col(position="fill") + geom_label(aes(y=ifelse(0<n,n,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),position=position_fill(vjust=0.5),na.rm=T,show.legend=F)
Warning message: Removed 3 rows containing missing values (position_stack).
ggplot(df) + aes(x="", y=n, fill=a) + geom_col() + geom_label(aes(y=ifelse(0<n,rev(cumsum(rev(n)))-n/2,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),nudge_x=0.6,na.rm=T,show.legend=F)
ggplot(df) + aes(x="", y=n, fill=a) + geom_col(position="fill") + geom_label(aes(y=ifelse(0<n,(rev(cumsum(rev(n)))-n/2)/sum(n),NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),nudge_x=0.6,na.rm=T,show.legend=F)
ggplot(df) + aes(x="", y=n, fill=a) + geom_col() + geom_label_repel(aes(y=ifelse(0<n,rev(cumsum(rev(n)))-n/2,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),nudge_x=0.6,na.rm=T,show.legend=F) + coord_polar(theta="y")
> theme_void function (base_size = 11, base_family = "", base_line_size = base_size/22, base_rect_size = base_size/22) { half_line <- base_size/2 theme(line = element_blank(), rect = element_blank(), text = element_text(family = base_family, face = "plain", colour = "black", size = base_size, lineheight = 0.9, hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(), debug = FALSE), axis.text = element_blank(), axis.title = element_blank(), axis.ticks.length = unit(0, "pt"), axis.ticks.length.x = NULL, axis.ticks.length.x.top = NULL, axis.ticks.length.x.bottom = NULL, axis.ticks.length.y = NULL, axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL, legend.box = NULL, legend.key.size = unit(1.2, "lines"), legend.position = "right", legend.text = element_text(size = rel(0.8)), legend.title = element_text(hjust = 0), strip.text = element_text(size = rel(0.8)), strip.switch.pad.grid = unit(half_line/2, "pt"), strip.switch.pad.wrap = unit(half_line/2, "pt"), panel.ontop = FALSE, panel.spacing = unit(half_line, "pt"), plot.margin = unit(c(0, 0, 0, 0), "lines"), plot.title = element_text(size = rel(1.2), hjust = 0, vjust = 1, margin = margin(t = half_line)), plot.subtitle = element_text(hjust = 0, vjust = 1, margin = margin(t = half_line)), plot.caption = element_text(size = rel(0.8), hjust = 1, vjust = 1, margin = margin(t = half_line)), plot.tag = element_text(size = rel(1.2), hjust = 0.5, vjust = 0.5), plot.tag.position = "topleft", complete = TRUE) } <bytecode: 0x000001e83ed68968> <environment: namespace:ggplot2>を参考にして
ggplot(df) + aes(x="", y=n, fill=a) + geom_col() + geom_label_repel(aes(y=ifelse(0<n,rev(cumsum(rev(n)))-n/2,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),nudge_x=0.6,na.rm=T,show.legend=F) + coord_polar(theta="y") + theme(legend.position="none",panel.background=NULL,axis.title=element_blank(),axis.text=element_blank(),rect=element_blank(),line=element_blank())くらいしておけば、おおよそいらない物は消えると思う。
mycolor = rev(c( "#ffa0a0", "#ffe0a0", "#ffffe0", "#c0ffc0", "#a0ffff", "#a0e0ff", "#a0c0ff", "#c0c0ff", "#e0c0ff", "#ffc0ff", "#e0e0e0"))
ggplot(df) + aes(x="", y=n, fill=a) + geom_col(colour="black") + geom_label_repel(aes(y=ifelse(0<n,rev(cumsum(rev(n)))-n/2,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),nudge_x=0.6,na.rm=T,show.legend=F) + coord_polar(theta="y") + scale_fill_manual(values=rev(mycolor)) + theme(legend.position="none",panel.background=NULL,axis.title=element_blank(),axis.text=element_blank(),rect=element_blank(),line=element_blank())
ggplot(df) + aes(x="", y=n, fill=a) + geom_col(colour="black") + geom_label_repel(aes(y=ifelse(0<n,rev(cumsum(rev(n)))-n/2,NA),label=ifelse(0<n,sprintf("%s:%2d\n%4.1f%%",a,n,100*n/sum(n)),"")),nudge_x=0.6,na.rm=T,show.legend=F) + coord_polar(theta="y",direction=-1) + scale_fill_manual(values=rev(mycolor)) + theme(legend.position="none",panel.background=NULL,axis.title=element_blank(),axis.text=element_blank(),rect=element_blank(),line=element_blank())