簡體   English   中英

r餅圖標簽與ggplot2重疊

[英]r pie chart labels overlap ggplot2

我正在嘗試制作包含多個切片的餅圖,其中許多切片的值較低。 問題是,當我使圖表上的大多數標簽相互重疊時。

圖形是這樣的:

圖形

數據:

           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

我試圖在這里找到解決方案,但是沒有運氣。 有人有主意嗎?

這是嘗試使用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_reorderdf$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)

然后是繪圖(請注意,當我現在通過geom_label_repel()添加標簽時,我將theme(axis.x.text)更改為element_blank()

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

餅形圖

這是該圖的另一個版本,您無需為標簽提供另一個數據框。 我選擇將標簽放在條形圖之前,但這取決於您。 請注意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

條形圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM