代码之家  ›  专栏  ›  技术社区  ›  paolotroia

ggplotly第一个条形标签半隐藏在水平条形图中

  •  0
  • paolotroia  · 技术社区  · 7 月前

    我无法理解为什么我的ggplot第一个条形标签在下面的水平条形图中半隐藏。

    我不想缩小标签的尺寸,因为它们看起来太小了。我试着增加一个边距,但无济于事。

    你能帮我吗?

    Link to the dataset

    # Import dataset
    db <- read_excel("dataset.xlsx")
    options(scipen = 999)
    
    # Rename variables
    colnames(db) <- c("rank",
                      "name",
                      "sport",
                      "totalEarnings",
                      "on-the-field_Earnings",
                      "off-the-field_Earnings")
    
    # Format numbers
    custom_number_format <- function(x){ifelse(x > 999999,paste(format(round((x/1000000))),"M"),format(round(x)))}
    
    # Create text column in dataset to display the tooltip
    db <- db %>%
      mutate(text = paste(
        "<span style='font-size:14px;'><b>", name, "</b></span><br>",
        "\nOn Field Earnings:", "<b> $", custom_number_format(`on-the-field_Earnings`), "</b>",
        "\nOff Field Earnings:", "<b> $",custom_number_format(`off-the-field_Earnings`), "</b><br>",
        "<span style='font-size:16px;'> \nTotal Earnings:</span>", "<b> $", custom_number_format(totalEarnings), "</b>"))
    
    
    # Design Bar chart
    ggp <- db %>%
      mutate(name = fct_reorder(name, totalEarnings)) %>%
      ggplot(aes(totalEarnings, name, fill = totalEarnings, text = text)) +
      geom_col(width = 0.7, show.legend = FALSE) +
      scale_x_continuous(limits=c(0, 160000000), expand = c(0, 0)) +
      scale_y_discrete(expand = expansion(mult = 0, 0.05)) +
      scale_fill_gradient(high = "#24336a", low = "#2296cf") +
      theme(
        plot.background = element_blank(),
        panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank()
      ) +
      geom_text(aes(label = custom_number_format(totalEarnings)), size = 3)
    
    ggp <- ggplotly(ggp, tooltip = "text") %>%
      config(displayModeBar = FALSE)
    
    ggp <- ggp %>%
      layout(
        hoverlabel = list(bgcolor = "white")
      )
    
    ggp$x$data[[44]]$textposition <- "right"
    ggp
    

    enter image description here

    1 回复  |  直到 7 月前
        1
  •  0
  •   stefan    7 月前

    问题在于您为y标尺设置的小扩展。增加扩展,你应该没问题,例如在下面的代码中,我在y标尺的任一侧使用了1的加性扩展,但也可以使用例如仅在上端增加它。 scale_y_discrete(expand = c(0, 0.05, 0, 1)) .

    使用一些虚假的示例数据:

    library(plotly, warn=FALSE)
    #> Loading required package: ggplot2
    library(forcats)
    
    set.seed(123)
    
    db <- data.frame(
      name = sample(c(LETTERS, letters), 43),
      totalEarnings = seq(1e5, 136e6, length.out = 43)
    )
    
    ggp <- db %>%
      mutate(name = fct_reorder(name, totalEarnings)) %>%
      ggplot(aes(totalEarnings, name, fill = totalEarnings)) +
      geom_col(width = 0.7, show.legend = FALSE) +
      scale_x_continuous(limits = c(0, 160000000), expand = c(0, 0)) +
      scale_y_discrete(expand = c(0, 1)) +
      scale_fill_gradient(high = "#24336a", low = "#2296cf") +
      theme(
        plot.background = element_blank(),
        panel.background = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank()
      ) +
      geom_text(aes(label = custom_number_format(totalEarnings)), size = 3) +
      coord_cartesian(clip = "off")
    
    ggp <- ggplotly(ggp, tooltip = "text") %>%
      config(displayModeBar = FALSE)
    
    ggp <- ggp %>%
      layout(
        hoverlabel = list(bgcolor = "white")
      )
    
    ggp$x$data[[44]]$textposition <- "right"
    
    ggp