ggplot,facet,piechart:将文本放置在饼图切片的中间 [英] ggplot, facet, piechart: placing text in the middle of pie chart slices

查看:154
本文介绍了ggplot,facet,piechart:将文本放置在饼图切片的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot制作一个刻面的饼图,并面临将文本放在每个片段中间的问题:

  dat = read.table(text =渠道交易量cnt 
代理高8344
代理中5448
代理低23823
KIOSK高19275
KIOSK中13554
KIOSK low 38293,header = TRUE)

vis = ggplot(data = dat,aes(x = factor(1),y = Cnt,fill = Volume))+
geom_bar(stat =identity,position =fill)+
coord_polar(theta =y)+
facet_grid(Channel〜。)+
geom_text(aes(x =因子(1),y = Cnt,label = Cnt,ymax = Cnt),
position = position_fill(width = 1))

输出:
p>

geom_te有哪些参数xt 应该调整,以便将数字标签放置在饼图切片的中间位置。



相关问题是, plyr dplyr

a> packages:

第1步:为每个频道创建位置变量

<$ (dat,ave(Cnt,Channel,FUN = function(x)cumsum(x) - 0.5 * x))的基数R
dat $ pos<

#with data.table package
library(data.table)
setDT(dat)
dat <-dat [,pos:= cumsum(Cnt )-0.5 * Cnt,by =Channel]

#与plyr软件包
library(plyr)
dat < - ddply(dat,。(Channel),转换,pos = cumsum(Cnt)-0.5 * Cnt)

#与dplyr包
库(dplyr)
dat < - dat%>>%group_by(Channel )%>%mutate(pos = cumsum(Cnt)-0.5 * Cnt)

第2步:创建分面图

  library(ggplot2)
ggplot(data = dat)+
geom_bar(aes(x =,y = Cnt,fill = $)$ + b $ b geom_text(x =,y = pos,label = Cnt))+
coord_polar(theta =y)+
facet_grid(Channel〜。,scales =free)

结果: strong>




I'm trying to produce a facetted pie-chart with ggplot and facing problems with placing text in the middle of each slice:

dat = read.table(text = "Channel Volume Cnt
                         AGENT   high   8344
                         AGENT medium   5448
                         AGENT    low  23823
                         KIOSK   high  19275
                         KIOSK medium  13554
                         KIOSK    low  38293", header=TRUE)

vis = ggplot(data=dat, aes(x=factor(1), y=Cnt, fill=Volume)) +
  geom_bar(stat="identity", position="fill") +
  coord_polar(theta="y") +
  facet_grid(Channel~.) +
  geom_text(aes(x=factor(1), y=Cnt, label=Cnt, ymax=Cnt), 
            position=position_fill(width=1))

The output:

What parameters of geom_text should be adjusted in order to place numerical labels in the middle of piechart slices?

Related question is Pie plot getting its text on top of each other but it doesn't handle case with facet.

UPDATE: following Paul Hiemstra advice and approach in the question above I changed code as follows:

---> pie_text = dat$Cnt/2 + c(0,cumsum(dat$Cnt)[-length(dat$Cnt)])

     vis = ggplot(data=dat, aes(x=factor(1), y=Cnt, fill=Volume)) +
     geom_bar(stat="identity", position="fill") +
     coord_polar(theta="y") +
     facet_grid(Channel~.) +
     geom_text(aes(x=factor(1), 
--->               y=pie_text, 
                   label=Cnt, ymax=Cnt), position=position_fill(width=1))

As I expected tweaking text coordiantes is absolute but it needs be within facet data:

解决方案

NEW ANSWER: With the introduction of ggplot2 v2.2.0, position_stack() can be used to position the labels without the need to calculate a position variable first. The following code will give you the same result as the old answer:

ggplot(data = dat, aes(x = "", y = Cnt, fill = Volume)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  facet_grid(Channel ~ ., scales = "free")

To remove "hollow" center, adapt the code to:

ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) + 
  geom_bar(stat = "identity") +
  geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
  scale_x_continuous(expand = c(0,0)) +
  coord_polar(theta = "y") +
  facet_grid(Channel ~ ., scales = "free")


OLD ANSWER: The solution to this problem is creating a position variable, which can be done quite easily with base R or with the data.table, plyr or dplyr packages:

Step 1: Creating the position variable for each Channel

# with base R
dat$pos <- with(dat, ave(Cnt, Channel, FUN = function(x) cumsum(x) - 0.5*x))

# with the data.table package
library(data.table)
setDT(dat)
dat <- dat[, pos:=cumsum(Cnt)-0.5*Cnt, by="Channel"]

# with the plyr package
library(plyr)
dat <- ddply(dat, .(Channel), transform, pos=cumsum(Cnt)-0.5*Cnt)

# with the dplyr package
library(dplyr)
dat <- dat %>% group_by(Channel) %>% mutate(pos=cumsum(Cnt)-0.5*Cnt)

Step 2: Creating the facetted plot

library(ggplot2)
ggplot(data = dat) + 
  geom_bar(aes(x = "", y = Cnt, fill = Volume), stat = "identity") +
  geom_text(aes(x = "", y = pos, label = Cnt)) +
  coord_polar(theta = "y") +
  facet_grid(Channel ~ ., scales = "free") 

The result:

这篇关于ggplot,facet,piechart:将文本放置在饼图切片的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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