geom_label() 将标签放在 R Shiny 中甜甜圈图的错误位置

geom_label() puts label in wrong location on a donut plot in R Shiny

我正在尝试在我的 R Shiny 应用程序中构建一个甜甜圈图,该图标记了与图部分对应的数字和百分比。但是,当我使用 geom_label() 时,它做了两件不受欢迎的事情。

  1. 它在图例中添加了一个“a”。
  2. 它将标签放置在与它们应该去的地方不对应的随机位置。

结果如下所示:

我对标签的位置很灵活,只要符合逻辑,但在理想情况下,最终结果应该是这样的(注意:这是一个示例图,没有使用下面的代码):

当然,标签中的“a”也会被删除。

可重现的例子:

library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            
        ),
        mainPanel(
            fluidRow(
                plotOutput("count_by_person")

            ))
    ))
server <- function(input, output) {
    
    
    output$count_by_person <- renderPlot({
        
        data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                       n = c(10, 30, 59, 1),
                       prop = c(10, 30, 59, 1))

        ggplot(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_label(aes(label = paste0(n, "\n", prop, "%"))) +
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)
    })
    
}
shinyApp(ui, server)

使用 geom_label_repel()

编辑

如下所示,我尝试 运行 使用 ggrepel 包中的 geom_label_repel() 而不是 geom_label() 的相同代码。虽然它得到了线,但它并没有改变标签在错误位置的事实。

library(shiny)
library(dplyr)
library(ggplot2)
library(Cairo)
library(ggrepel)
options(shiny.usecairo=T) #used to get the plot to look crisp in Shiny

my_colors <- c("#00A3AD", "#FF8200", "#753BBD", "#6CC24A")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            
        ),
        mainPanel(
            fluidRow(
                plotOutput("count_by_person")

            ))
    ))
server <- function(input, output) {
    
    
    output$count_by_person <- renderPlot({
        
        data <- tibble(name = c("Justin", "Corey", "Sibley", "Kate"),
                       n = c(10, 30, 59, 1),
                       prop = c(10, 30, 59, 1)) %>%
            dplyr::arrange(prop)

        ggplot(data, aes(x = 2, y = prop, fill = name)) +
            geom_bar(stat = "identity", color = "white") +
            coord_polar(theta = "y", start = 0)+
            geom_label_repel(aes(label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1) +
            scale_fill_manual(values = my_colors) +
            theme_void() +
            xlim(.5, 2.5)
    })
    
}
shinyApp(ui, server)

使用 geom_label_repel()

的图片

正如我在评论中链接的非常好的 by Andrey Kolyadin中所解释的那样,您需要预先计算文本的累积位置:

library(ggrepel)
data %>% 
  arrange(desc(name)) %>%
  mutate(text_y = cumsum(prop)-prop/2) %>%
ggplot(aes(x = 2, y = prop, fill = name)) +
   geom_bar(stat = "identity", color = "white") +
   coord_polar(theta = "y", start = 0)+
   geom_label_repel(aes(y = text_y, label = paste0(n, "\n", prop, "%")), force_pull = 100, nudge_x = 1) +
   scale_fill_manual(values = my_colors) +
   theme_void() +
   xlim(.5, 2.5)