R 绘制饼图
我有一个df(名为:R 绘制饼图,r,ggplot2,pie-chart,R,Ggplot2,Pie Chart,我有一个df(名为:cant\u masivos\u trim),如下所示: Descripcion Freq Cargos Jubilaciones 2185 Faltantes de Caja 470 ATM Diferencias 201 Previsiones Legales 34 Gastos Corresponsalía 22 Multas SICORE 19
cant\u masivos\u trim
),如下所示:
Descripcion Freq
Cargos Jubilaciones 2185
Faltantes de Caja 470
ATM Diferencias 201
Previsiones Legales 34
Gastos Corresponsalía 22
Multas SICORE 19
Sumarios 17
ATM Fraudes 10
Multas ANSeS 7
Multas AFIP 5
我想用ggplot2创建一个饼图,正如您在图像中看到的那样,我对标签有一个问题
我不知道为什么标签不在正确的位置,我也弄不明白
我正在使用的代码:
pmas <- ggplot(cant_masivos_trim, aes(x=1, y=Freq, fill=Descripcion)) +
geom_bar(stat="identity") +
ggtitle(paste("Cantidad de Reportes - Carga Masiva"))
pmas <- pmas + coord_polar(theta='y')
pmas <- ggplot(cant_masivos_trim, aes(x=1, Freq, fill=Descripcion)) +
ggtitle(paste("Cantidad de Reportes - Carga Masiva")) +
coord_polar(theta='y')
pmas <- pmas + geom_bar(stat="identity", color='black') + guides(fill=guide_legend(override.aes=list(colour=NA)))
pmas <- pmas + theme(axis.ticks=element_blank(), # the axis ticks
axis.title=element_blank(), # the axis labels
axis.text.y=element_blank()) # the 0.75, 1.00, 1.25 labels.
y.breaks <- cumsum(cant_masivos_trim$Freq) - cant_masivos_trim$Freq/2
pmas <- pmas +
# prettiness: make the labels black
theme(axis.text.x=element_text(color='black')) +
scale_y_continuous(
breaks=y.breaks, # where to place the labels
labels= (paste(cant_masivos_trim$Freq, percent(cant_masivos_trim$Freq/sum(cant_masivos_trim$Freq)), sep='\n'))) # the labels
pmas这与默认情况下绘制Pie切片的顺序有关。通过查看条形图(在coord_polar
之前)最容易看到发生了什么。ggplot2条形图根据描述因子的级别顺序从上到下绘制
在计算累计频率之前,要按description
对数据集进行排序,以计算现有的中断次数。要匹配默认ggplot2顺序,请按变量的相反顺序进行排序
cant_masivos_trim = cant_masivos_trim[rev(order(cant_masivos_trim$Descripcion)), ]
完成后,根据累积频率计算中断,并像以前一样将其居中
y.breaks = cumsum(cant_masivos_trim$Freq) - cant_masivos_trim$Freq/2
ggplot(cant_masivos_trim, aes(x =1, y = Freq, fill = Descripcion)) +
geom_bar(stat="identity") +
coord_polar(theta = "y") +
scale_y_continuous(breaks = y.breaks, # where to place the labels
labels = (paste(cant_masivos_trim$Freq,
scales::percent(cant_masivos_trim$Freq/sum(cant_masivos_trim$Freq)), sep='\n'))) +
theme(legend.position = "none",
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text.y=element_blank())