饼图标签重叠

时间:2019-08-13 21:03:00

标签: r ggplot2 r-markdown

无论我做什么,我似乎都找不到找到确保饼图标签的代码,不要与饼图或其他标签重叠的代码。

我以各种方式输入了geom_text_repel,并调整了vjust size force x,但没有任何效果。它适用于某些图表,而某些图表则无效。
pie chart

       ---
title: "Untitled"
date: "August 14, 2019"
output: html_document
---

```{r eval = TRUE, echo = FALSE, results = "asis", warning = FALSE,  message = FALSE, fig.height = 6.25, fig.width = 12}

library(plyr)
library(dplyr)
library(kableExtra)
library(scales)
library(ggplot2)
library(RODBC)
library(data.table)
library(DT)
library(treemapify)
library(devtools)
library(digest)
library(plotly)
library(shiny)
library(ggrepel)
library(expss)

rptyear <- 2018
colours <- c("A" = "royalblue3", "B" = "red", "C" = "gold", "D" = "green4")

Category <- c("A", "B", "C", "D")

premiumtable <- cbind(rep(c("A","B","C","D"),11), c(rep(2009,4),rep(2010,4),rep(2011,4),rep(2012,4),rep(2013,4),rep(2014,4),rep(2015, 4), rep(2016,4), rep(2017,4), rep(2018,4),rep(2019,4)), as.numeric(c(13223284, 3379574,721217, 2272843,14946074,4274769, 753797,2655032, 15997384, 4952687, 722556,3035566,16244348,5541543,887109,3299966,15841630,6303443,1101696,3751892,14993295, 6993626,1312650,4158196,13946038, 7081457,1317428,4711389, 12800640, 6923012, 1345159, 4911780, 12314663, 6449919, 1395973,5004046,12612704,6968110,1507382,5745079,15311213,8958588,1849069,6819488)))
colnames(premiumtable) <- c("Var1", "Var2", "Freq")
      currentPrem <- filter(as.data.table(premiumtable), Var2 == rptyear, Freq != 0)
      prempie <- ggplot(currentPrem, aes(x="", y = as.numeric(currentPrem$Freq), fill= Var1)) 
      prempie <- prempie + geom_bar(width = 1, stat = "identity", colour = "black") 
      prempie <- prempie + ggtitle(paste0("YTD Numbers:")) + coord_polar("y", start = 0) 
      prempie <- prempie + scale_fill_manual(values = colours)  
      prempie <- prempie + theme_void()+ theme(plot.title = element_text(face = "bold", size = 20, hjust = .5), legend.position = "none",  axis.title=element_text(size=20), axis.title.y = element_blank(), axis.title.x = element_blank())
      prempie <- prempie +  geom_text_repel(mapping = aes(label = paste0(Var1, "\n $",prettyNum(round(as.numeric(currentPrem$Freq)/1000), big.mark = ",")) , x =  2),position = position_stack( vjust = .5), size = 6, force = 5,direction = "both",  segment.size = 0)
```

1 个答案:

答案 0 :(得分:1)

感谢您提供工作数据/代码。如果您愿意使用软件包plotly,那么它非常擅长于立即生成饼图,并且比ggplot需要更少的摆弄。这是您的数据示例:

library(dplyr)
library(plotly)

#
rptyear <- 2018
colours <- c("A" = "royalblue3", "B" = "red", "C" = "gold", "D" = "green4")

# data
premiumtable <- data.frame(Var1 = rep(c("A","B","C","D"),11),
                           Var2 = c(rep(2009,4),rep(2010,4),rep(2011,4),rep(2012,4),rep(2013,4),rep(2014,4),rep(2015, 4),rep(2016,4), rep(2017,4),rep(2018,4),rep(2019,4)),
                           Freq = as.numeric(c(13223284, 3379574,721217, 2272843,14946074,4274769, 753797,2655032, 15997384, 4952687, 722556,3035566,16244348,5541543,887109,3299966,15841630,6303443,1101696,3751892,14993295, 6993626,1312650,4158196,13946038, 7081457,1317428,4711389, 12800640, 6923012, 1345159, 4911780, 12314663, 6449919, 1395973,5004046,12612704,6968110,1507382,5745079,15311213,8958588,1849069,6819488)))

# prepare plot data
currentPrem <-   
  premiumtable %>% 
  filter(Var2 == rptyear, Freq != 0) %>% 
  mutate(Freq = as.numeric(Freq))

# create plot labels
labels = paste0(currentPrem$Var1, "\n $",prettyNum(round(as.numeric(currentPrem$Freq)/1000), big.mark = ","))

# create plot
plot_ly(currentPrem,
        labels = ~labels,
        values = ~Freq, type = 'pie',
        textposition = 'outside',
        textinfo = 'label',
        colors = colours) %>%
  layout(title = paste("YTD Numbers:", rptyear),
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         showlegend = FALSE)

enter image description here