2017-03-07 2 views
0

Ich versuche, ein Kreisdiagramm mit mehreren Scheiben zu machen, und viele von ihnen haben niedrige Werte. Das Problem ist, dass wenn ich das Diagramm mache, die meisten Etiketten sich überlappen.r Kreisdiagramm Etiketten Überlappung ggplot2

Die Grafik ist dies:

graphic

Die Daten:

  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 

Und der 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 

Ich versuche, hier eine Lösung zu finden, haben aber keine Glück. Hat jemand eine Idee?

+0

Wenn Sie Etiketten oder Text anstelle von Achsenbeschriftungen hinzufügen, könnten Sie etwas entlang der Linien von [dieser] (http://stackoverflow.com/a/33337625/2461552) tun. Siehe auch Paket ggrepel; Ein Doughnut-Beispiel wird hier gezeigt (http://stackoverflow.com/a/38688118/2461552). – aosmith

Antwort

1

Hier ist ein Versuch mit ggrepel. Das Ergebnis für das Kreisdiagramm ist nicht wirklich hübsch, aber ich kann es nicht verbessern. Und danach biete ich eine andere Lösung ohne Tortendiagramme an.

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) 

ich ändern df$Descripcion zu einem Faktor, und bestellt von df$Freq, forcats::fct_reorder verwenden. Und dann ändere ich die Reihenfolge im Datenrahmen, damit die Funktion zum Positionieren der Beschriftungen korrekt funktioniert.

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 

Ich definiere dann einen anderen Datenrahmen, um die Etiketten zu platzieren. Ich wählte die x.breaks durch Versuch und Irrtum.

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) 

Und dann das Grundstück (beachten Sie, dass ich die theme(axis.x.text)-element_blank() geändert, wie ich die Etiketten durch geom_label_repel() jetzt hinzufügen)

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

Hier ist eine andere Version des Grundstücks, wo Sie müssen keinen weiteren Datenrahmen für die Etiketten bereitstellen. Ich entschied mich, die Etiketten vor die Bars zu legen, aber es liegt an dir. Beachten Sie die expand_limits(y = -150), um sicherzustellen, dass das Etikett sichtbar ist, und die coord_flip(), so dass die Etiketten besser lesbar sind. Ich verwende auch geom_col() anstelle von 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

Verwandte Themen