2017-03-07 216 views
0

我正在試圖製作一個餅圖與幾個切片,其中許多有低值。問題在於,當我製作圖表時,大多數標籤都會相互重疊。r餅圖標籤重疊ggplot2

圖形是這樣的:

graphic

數據:

  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 

,代碼:

#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 

我試圖在這裏找到一個解決方案,但都沒有運氣。有人有想法嗎?

+0

如果你添加標籤或文字代替軸標籤,你可以做沿東西線[這](http://stackoverflow.com/a/33337625/2461552)。另請參閱軟件包ggrepel;顯示了一個甜甜圈示例[here](http://stackoverflow.com/a/38688118/2461552)。 – aosmith

回答

1

這是一個嘗試使用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) 

我改變df$Descripcion的因素,並通過df$Freq訂購,使用forcats::fct_reorder。然後我改變數據框中的順序,所以用於定位標籤的功能正常工作。

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 

然後我定義另一個數據框來放置標籤。我通過試驗和錯誤選擇了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) 

然後劇情(請注意,我改變了theme(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 

Pie Chart

這裏是小區,這裏的另一個版本您不需要爲標籤提供另一個數據框。我選擇在酒吧之前放置標籤,但取決於您。請注意0​​以確保標籤可見,並且標籤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 

Bar chart