小件饼图上的标签(ggplot) [英] labels on the pie chart for small pieces (ggplot)

查看:1086
本文介绍了小件饼图上的标签(ggplot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot中创建饼图



我的数据:

  lab <-c(a,b,c,d,e,f,g,h)
百分比<-c (50,20,10,10,2,2,2,2)
df.prison< - data.frame(lab,percentage)
df.prison $ crime< - factor(df .prison $ lab,levels = rev(levels(df.prison $ lab)))
labels.prison< - paste(lab, - ,percentage,%,sep =)

Plot:

  plot <-ggplot(data = df.prison,aes(x = factor(1),y = percentage,fill = factor(lab)))+ 
geom_bar(width = 1,stat =身份)+
coord_polar(theta =y)+
ylab()+
xlab()+
labs(fill =)+
主题(axis.ticks = element_blank(),panel.grid = element_blank(),axis.text = element_blank())+
geom_text(aes(y = percent / 2 + c(0,cumsum(百分比)[ - 长度(百分比)]),label = labels.prison))
plot

1.我不想有传说(因为标签很短(一个字母),我想把它们放在饼图上
2.是否可以在小块旁边放置小块(小于百分比小)的标签,因为标签太大而无法放入内部这小块。比如像这样:




I want to make pie chart in ggplot

My data:

lab <- c("a", "b", "c", "d", "e", "f", "g", "h")
percentage <- c(50, 20, 10, 10, 2, 2,2,2)
df.prison <- data.frame(lab, percentage)
df.prison$crime <- factor(df.prison$lab, levels=rev(levels(df.prison$lab)))
labels.prison <- paste(lab, "-", percentage, "%", sep="")

Plot:

plot <- ggplot(data=df.prison, aes(x=factor(1), y=percentage, fill=factor(lab))) +
   geom_bar(width=1, stat="identity") +
   coord_polar(theta="y") +
   ylab("") +
   xlab("") +
   labs(fill="") +
   theme(axis.ticks = element_blank(), panel.grid  = element_blank(), axis.text = element_blank()) +
   geom_text(aes(y = percentage/2 + c(0, cumsum(percentage)[-length(percentage)]), label=labels.prison))
plot

I have two problems with this plot: 1. I don't want to have legend (because labels are very short (one letter) and I want to have them on the pie chart 2. Is it possible to place labels for the small pieces (smaller than few percentages) next to the plot, because the label in too big to place in inside this small piece. For example like here:

http://www.conceptdraw.com/How-To-Guide/picture/Pie-chart-Sector-weightings.png

Thanks for any advise :)

解决方案

legend.position = 'none' will remove the legend. You can adjust the labels by adjusting the x value with the mapping for geom_text.

library(ggplot2)

lab <- c("a", "b", "c", "d", "e", "f", "g", "h")
percentage <- c(50, 20, 10, 10, 2, 2, 2, 2)
df.prison <- data.frame(lab, percentage)
df.prison$crime <- factor(df.prison$lab, levels=rev(levels(df.prison$lab)))
labels.prison <- paste(lab, "-", percentage, "%", sep="")

ggplot(data=df.prison, aes(x=factor(1), y=percentage, fill=factor(lab))) +
   geom_bar(width=1, stat="identity") +
   coord_polar(theta="y") +
   ylab("") +
   xlab("") +
   labs(fill="") +
   theme(legend.position = "none",   ### Solution to part 1, no legend
         axis.ticks = element_blank(), 
         panel.grid = element_blank(), 
         axis.text  = element_blank()) +
   geom_text(aes(x = c(1, 1, 1, 1, 1.2, 1.3, 1.4, 1.5), # Solution for part 2,
                 y = percentage / 2 + c(0, cumsum(percentage)[-length(percentage)]), 
                 label=labels.prison))

这篇关于小件饼图上的标签(ggplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆