问题在于您为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