简体   繁体   English

R plotly 堆叠条形图文本标签未与条对齐

[英]R plotly stacked bar chart text labels not aligned to bars

I have spent way too long trawling through google/stack overflow trying to find an answer but no luck!我花了太长时间在 google/stack overflow 上搜索,试图找到答案,但没有成功!

Essentially, I have created a stacked bar chart, but the text labels overlaid do not align with the correct bars.本质上,我创建了一个堆积条形图,但覆盖的文本标签与正确的条形不对齐。 I have tried using text = ~value , the annotations parameter and many more... but no luck.我试过使用text = ~valueannotations参数等等......但没有运气。

Here's my latest version of the code and a picture example.这是我最新版本的代码和图片示例。 I'm assuming it must be a simple solution but I can't figure it out!我假设它一定是一个简单的解决方案,但我无法弄清楚!

t <- list(
  size = 12,
  color = 'black')

fig <- df4 %>% plot_ly(x = ~variable, y = ~value, type = 'bar', 
        name = ~Country, color = ~Country) %>%
  layout(yaxis = list(title = '% of Value'), 
         xaxis = list(title = 'Metric'), 
         barmode = 'stack', 
         title = "Proportion of value across countries") %>%
  add_text(text=~value, textposition="middle center",
           showlegend = F, textfont = t)

fig

在此处输入图像描述

Managed to solve it:设法解决它:

fig <- df4 %>% plot_ly(x = ~variable, y = ~value, type = 'bar',
                             text = paste(df4$value,"%"),
                             textposition = "outside",
                             color = ~Country,
                             textfont = t)%>%
  layout(yaxis = list(title = '% of Value'), 
         xaxis = list(title = 'Country'),
         title = "Proportion of value across countries",
         barmode = 'stack') 
fig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM