I'm trying to make a pie chart with several slices, and many of them have low values. The problem is that when I make the chart most of the labels overlap each other.
我正在尝试制作一个包含多个切片的饼图,其中许多切片的值很低。问题是,当我制作图表时,大多数标签相互重叠。
The graphic is this:
图形是这样的:
The data:
数据:
Descripcion Freq
Sumarios 17
Previsiones Legales 34
Multas SICORE 19
Multas ANSeS 7
Multas AFIP 5
Gastos Corresponsalía 22
Faltantes de Caja 470
Cargos Jubilaciones 2185
ATM Fraudes 10
ATM Diferencias 201
And the code:
和代码:
#armo el grafico
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
I try to find a solution here, but have no luck. Does anybody have an idea?
我试着在这里找到解决方案,但没有运气。有人有想法吗?
1 个解决方案
#1
2
Here is an attempt using ggrepel
. The result for the pie chart is not really pretty, but I can't improve it. And afterwards, I provide another solution without pie charts at all.
这是使用ggrepel的尝试。饼图的结果并不是很漂亮,但我无法改进它。然后,我提供了另一种没有饼图的解决方案。
library(ggplot2)
library(tibble)
library(scales)
library(ggrepel)
library(forcats)
df <- tribble(
~Descripcion, ~Freq,
"Sumarios", 17,
"Previsiones Legales", 34,
"Multas SICORE", 19,
"Multas ANSeS", 7,
"Multas AFIP", 5,
"Gastos Corresponsalía", 22,
"Faltantes de Caja", 470,
"Cargos Jubilaciones", 2185,
"ATM Fraudes", 10,
"ATM Diferencias", 201)
I change df$Descripcion
to a factor, and ordered by df$Freq
, using forcats::fct_reorder
. And then I change the order in the data frame, so the function to position the labels works correctly.
我将df $ Descripcion改为一个因子,并使用forcats :: fct_reorder以df $ Freq排序。然后我更改了数据框中的顺序,因此定位标签的功能正常工作。
df$Descripcion <- fct_reorder(df$Descripcion, df$Freq)
df <- df[order(df$Freq, decreasing = TRUE), ]
df
# A tibble: 10 × 2
# Descripcion Freq
# <fctr> <dbl>
# 1 Sumarios 17
# 2 Previsiones Legales 34
# 3 Multas SICORE 19
# 4 Multas ANSeS 7
# 5 Multas AFIP 5
# 6 Gastos Corresponsalía 22
# 7 Faltantes de Caja 470
# 8 Cargos Jubilaciones 2185
# 9 ATM Fraudes 10
# 10 ATM Diferencias 201
I then define another data frame to place the labels. I chose the x.breaks through trial and error.
然后我定义另一个数据框来放置标签。我通过反复试验选择了x.breaks。
my_labels <- tibble(x.breaks = seq(1, 1.5, length.out = 10),
y.breaks = cumsum(df$Freq) - df$Freq/2,
labels = paste(df$Freq, percent(df$Freq/sum (df$Freq)), sep='\n'),
Descripcion = df$Descripcion)
And then the plot (note that I changed the theme(axis.x.text)
to element_blank()
as I add the labels through geom_label_repel()
now)
然后是情节(请注意我将主题(axis.x.text)更改为element_blank(),因为我现在通过geom_label_repel()添加标签)
pmas <- ggplot(df, aes(x = 1, y = Freq, fill = Descripcion)) +
ggtitle(paste("Cantidad de Reportes - Carga Masiva")) +
geom_bar(stat="identity", color='black') +
coord_polar(theta='y') +
guides(fill=guide_legend(override.aes=list(colour=NA)))+
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.
axis.text.x = element_blank(),
panel.grid = element_blank()) +
scale_fill_brewer(palette = "Set3", direction = -1)+
geom_label_repel(data = my_labels, aes(x = x.breaks, y = y.breaks,
label = labels, fill = Descripcion),
label.padding = unit(0.1, "lines"),
size = 2,
show.legend = FALSE,
inherit.aes = FALSE)
pmas
Here is another version of the plot, where you do not need to provide another data frame for the labels. I chose to put the labels before the bars, but it is up to you. Note the expand_limits(y = -150)
to ensure that the label is visible, and the coord_flip()
so as the labels are more readable. I also use geom_col()
in place of geom_bar(stat = "identity")
.
这是该图的另一个版本,您不需要为标签提供另一个数据框。我选择将标签放在酒吧前面,但这取决于你。请注意expand_limits(y = -150)以确保标签可见,并注意coord_flip()以使标签更具可读性。我还使用geom_col()代替geom_bar(stat =“identity”)。
pmas2 <- ggplot(data = df, aes(x = Descripcion, y = Freq)) +
geom_col(aes(fill = Descripcion) , show.legend = FALSE) +
ggtitle(paste("Cantidad de Reportes - Carga Masiva")) +
coord_flip() +
geom_label(aes(label = paste(df$Freq, percent(df$Freq/sum(df$Freq)), sep = "\n"),
y = -150, fill = Descripcion),
show.legend = FALSE,
size = 3, label.padding = unit(0.1, "lines")) +
expand_limits(y = -150) +
scale_fill_brewer(palette = "Set3", direction = -1)
pmas2
#1
2
Here is an attempt using ggrepel
. The result for the pie chart is not really pretty, but I can't improve it. And afterwards, I provide another solution without pie charts at all.
这是使用ggrepel的尝试。饼图的结果并不是很漂亮,但我无法改进它。然后,我提供了另一种没有饼图的解决方案。
library(ggplot2)
library(tibble)
library(scales)
library(ggrepel)
library(forcats)
df <- tribble(
~Descripcion, ~Freq,
"Sumarios", 17,
"Previsiones Legales", 34,
"Multas SICORE", 19,
"Multas ANSeS", 7,
"Multas AFIP", 5,
"Gastos Corresponsalía", 22,
"Faltantes de Caja", 470,
"Cargos Jubilaciones", 2185,
"ATM Fraudes", 10,
"ATM Diferencias", 201)
I change df$Descripcion
to a factor, and ordered by df$Freq
, using forcats::fct_reorder
. And then I change the order in the data frame, so the function to position the labels works correctly.
我将df $ Descripcion改为一个因子,并使用forcats :: fct_reorder以df $ Freq排序。然后我更改了数据框中的顺序,因此定位标签的功能正常工作。
df$Descripcion <- fct_reorder(df$Descripcion, df$Freq)
df <- df[order(df$Freq, decreasing = TRUE), ]
df
# A tibble: 10 × 2
# Descripcion Freq
# <fctr> <dbl>
# 1 Sumarios 17
# 2 Previsiones Legales 34
# 3 Multas SICORE 19
# 4 Multas ANSeS 7
# 5 Multas AFIP 5
# 6 Gastos Corresponsalía 22
# 7 Faltantes de Caja 470
# 8 Cargos Jubilaciones 2185
# 9 ATM Fraudes 10
# 10 ATM Diferencias 201
I then define another data frame to place the labels. I chose the x.breaks through trial and error.
然后我定义另一个数据框来放置标签。我通过反复试验选择了x.breaks。
my_labels <- tibble(x.breaks = seq(1, 1.5, length.out = 10),
y.breaks = cumsum(df$Freq) - df$Freq/2,
labels = paste(df$Freq, percent(df$Freq/sum (df$Freq)), sep='\n'),
Descripcion = df$Descripcion)
And then the plot (note that I changed the theme(axis.x.text)
to element_blank()
as I add the labels through geom_label_repel()
now)
然后是情节(请注意我将主题(axis.x.text)更改为element_blank(),因为我现在通过geom_label_repel()添加标签)
pmas <- ggplot(df, aes(x = 1, y = Freq, fill = Descripcion)) +
ggtitle(paste("Cantidad de Reportes - Carga Masiva")) +
geom_bar(stat="identity", color='black') +
coord_polar(theta='y') +
guides(fill=guide_legend(override.aes=list(colour=NA)))+
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.
axis.text.x = element_blank(),
panel.grid = element_blank()) +
scale_fill_brewer(palette = "Set3", direction = -1)+
geom_label_repel(data = my_labels, aes(x = x.breaks, y = y.breaks,
label = labels, fill = Descripcion),
label.padding = unit(0.1, "lines"),
size = 2,
show.legend = FALSE,
inherit.aes = FALSE)
pmas
Here is another version of the plot, where you do not need to provide another data frame for the labels. I chose to put the labels before the bars, but it is up to you. Note the expand_limits(y = -150)
to ensure that the label is visible, and the coord_flip()
so as the labels are more readable. I also use geom_col()
in place of geom_bar(stat = "identity")
.
这是该图的另一个版本,您不需要为标签提供另一个数据框。我选择将标签放在酒吧前面,但这取决于你。请注意expand_limits(y = -150)以确保标签可见,并注意coord_flip()以使标签更具可读性。我还使用geom_col()代替geom_bar(stat =“identity”)。
pmas2 <- ggplot(data = df, aes(x = Descripcion, y = Freq)) +
geom_col(aes(fill = Descripcion) , show.legend = FALSE) +
ggtitle(paste("Cantidad de Reportes - Carga Masiva")) +
coord_flip() +
geom_label(aes(label = paste(df$Freq, percent(df$Freq/sum(df$Freq)), sep = "\n"),
y = -150, fill = Descripcion),
show.legend = FALSE,
size = 3, label.padding = unit(0.1, "lines")) +
expand_limits(y = -150) +
scale_fill_brewer(palette = "Set3", direction = -1)
pmas2