How to delete a "0%" label from an horizontal bar plot in plotly on R

问题描述 投票:0回答:0

在这个水平条形图上,我试图不显示 0%,我尝试了很多,但我被卡住了。

The picture

x1 <- c(21, 24, 27, 29)

data <- data.frame(y, x1, x2, x3, x4, x5)

# labeling the percentages of each bar (x_axis)
fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
                  x = x1 / 2, y = y,
                  text = paste(data[,"x1"], '%'),
                  font = list(family = 'Arial', size = 12,
                            color = 'rgb(248, 248, 255)'),
                  showarrow = FALSE) 

我试过了,但没用,如果列表中只有一个零,它会使列表中的所有元素从绘图中消失,我只尝试使 0 不出现并保留其他元素列表

for (i in 1:length(x1)) {
    fig <- fig %>% add_annotations(xref = 'x', yref = 'y',
                                   x = x1 / 2, y = y,
                                   text = paste0("<b>", if(data[i,"x1"] != 0) data[i,"x1"], else [] ,'%', "</b>"),
                                   font = list(family = 'calibri', size = 12,
                                               color = 'black'),
                                   showarrow = FALSE)}
r plotly rstudio
© www.soinside.com 2019 - 2024. All rights reserved.