2017-03-07 1 views
0

여러 조각으로 파이 차트를 만들려고하고 있으며 그 중 많은 차트에 작은 값이 있습니다. 문제는 차트를 만들 때 대부분의 레이블이 서로 겹치는 것입니다.원형 차트 레이블 겹침 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 패키지를 참조하십시오. 도너츠 예제가 여기에 나와 있습니다 (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을 변경하고 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 

그런 다음 레이블을 배치 할 다른 데이터 프레임을 정의합니다. 시행 착오를 통해 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) 

그리고 줄거리는

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

플롯의 또 다른 버전입니다 (지금 geom_label_repel() 통해 레이블을 추가로 내가 element_blank()theme(axis.x.text) 변경 참고) 레이블에 다른 데이터 프레임을 제공 할 필요가 없습니다. 나는 술집 앞에서 레이블을 붙이기로 결심했다. 그러나 그것은 당신에게 달렸다. 레이블이 표시되도록 expand_limits(y = -150)을 확인하고 레이블이 읽기 쉽도록 coord_flip()을 확인하십시오. 또한 geom_bar(stat = "identity") 대신 geom_col()을 사용합니다.

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

관련 문제