繁体   English   中英

更改 Plotly 漏斗 (R) 中的数字格式

[英]Change the number formatting in Plotly funnel (R)

我在下面有一个漏斗 plot:

val = as.list(df[,2])
txt = as.list(df[,1])

library(plotly)


fig <- plot_ly()

fig <- fig  %>%  
  add_trace(
    type = "funnel",
    textinfo = "value+percent initial",
    opacity = 0.65,
    x = val,
    y = txt,
    connector = list(line = list(color = "royalblue", dash = "dot", width = 3)), 
    colorbar = list(exponentformat = 'none')
  )

fig <- fig %>% 
  layout(
    hovermode = "closest"
  )

fig

我想将下面的格式更改为另一个选项。 最好是一个完整的整数。

在此处输入图像描述

我想要整个数字,而不是 11k。 谢谢!

可以在 xaxis 或 yaxis 中使用 hoverformat 参数

plot_ly(data=dt_toPlot,type="funnel",
               x=~n,y=~status,opacity=0.7,
               hoverinfo="name+x+y+percent initial+percent previous")%>%
          layout(yaxis = list(title="",categoryarray = ~status),
                 xaxis = list(hoverformat = '.0f'))%>%
            config(displayModeBar = FALSE),

我有同样的疑问,在R中很难找到这个问题的答案。但是,我在Python中查看了同样的问题,然后找到了解决方案。

fig <- fig  %>%  
  add_trace(
    type = "funnel",
    textinfo = "value+percent initial",
    opacity = 0.65,
    x = val,
    y = txt,
    connector = list(line = list(color = "royalblue", dash = "dot", width = 3)), 
    colorbar = list(exponentformat = 'none'),
    textinfo: "x",
    texttemplate = '%{value:,.3~s}'
  )

fig <- fig %>% 
  layout(
    hovermode = "closest"
  )

fig

我只是在代码上添加了两行,一行用于指示要显示的内容,另一行用于显示我想要的格式。

重要的是要提到这种格式 (%{value:,.3~s}) 的行为是这样的:

示例:20200 -> 20.2k

关于这个问题,你说你想要一个 integer 号码,所以它需要改变格式类型,这是基于 d3-format 的语法。 你可以查看: https://github.com/d3/d3-format

另一种我喜欢的替代方法是使用我希望在图形和 hover 上同时出现的文本向我的数据添加列。添加列后,您可以使用text=参数指定要显示的列包含您希望在 hover 上显示的文本,以及用于指定包含您希望在图形上显示的文本的列的texttemplate=参数。

以这个数据为例:

fundat<-data.frame(Stage=c("1. Beginning","2. Act 2","3. Act 3","4. Act 4","5. End"),Amount=c(948000,872000,414000,297000,119000))

假设我们想要:

  • 数字不会给您 11.995K 待遇。
  • 在某些漏斗阶段(或全部,世界是你的牡蛎)的文本中出现的特定附加指标。
  • Hover 文本包含我们想要的任何任意信息。

下面是一些添加示例文本列的代码:

fundat2<-fundat%>%
 mutate(StandardTxt=ifelse(Stage %in% c("1. Beginning"),Amount,
         ifelse(Stage %in% c("2. Act 2"),
         paste0(Amount,"<br> (",round((Amount[Stage %in% c("2. Act 2")]/Amount[Stage %in% 
c("1. Beginning")])*100,1),"% of Beginnings)"),
          ifelse(Stage %in% c("3. Act 3"),
          paste0(Amount,"<br> (",round((Amount[Stage %in% c("3. Act 3")]/Amount[Stage %in% c("2. Act 2")])*100,1),"% of Act 2 & <br>",round((Amount[Stage %in% c("3. Act 
3")]/Amount[Stage %in% c("4. Act 4")])*100,1), " of Act 4)"),Amount))))%>%
mutate(HoverTxt=paste0("The first digit of the amount is 
",paste0(substr(Amount,1,1))))

这给了我们一个 dataframe 看起来像这样: 在此处输入图像描述

我们现在创建了一个名为 StandardTxt 的列,其中包含我们希望在图形上显示的文本,以及一个名为 HoverTxt 的列,其中包含我们希望在 hover 上显示的文本。请注意<br>是一个 html 换行符,并且在呈现 plot 时,中断将出现在您的文本中。

要使用 plot 中的文本:

plot_ly(fundat2,type='funnel',x=~Amount,y=~Stage,text=~HoverTxt,hoverinfo="text",texttemplate=~StandardTxt)

我们在“第 2 幕”漏斗阶段生成的 plot 和 cursor 显示如下:

在此处输入图像描述

希望这可以帮助!

注意:确保你的数字在你的绘图之前没有得到科学记数法处理!

当 R 已经将您的数字转换为科学记数法时

考虑

fundat<-data.frame(Stage=c("1. Beginning","2. Act 2","3. Act 3","4. Act 4","5. End"),Amount=c(948000000,872000000,414000000,297000000,119000000))

我们处理的数字比上面的例子大得多。 当我查看 fundat 时,我看到:

在此处输入图像描述

这个科学记数法将通过我们的示例一直延续到我们的 plot,要解决这个问题,您可以使用format()删除科学记数法。

fundat2<-fundat%>%
mutate(Amount=format(Amount,scientific=FALSE))  

根据我的经验,这会破坏我用于文本字符串的代码,因此我不得不在百分比计算中使用as.numeric() “Amount”的引用包装起来,如下所示:

fundat2<-fundat%>%
  mutate(Amount=format(Amount,scientific=FALSE))%>%
  mutate(StandardTxt=ifelse(Stage %in% c("1. Beginning"),Amount,
         ifelse(Stage %in% c("2. Act 2"),paste0(Amount,"<br>(",round((as.numeric(Amount[Stage %in% c("2. Act 2")])/as.numeric(Amount[Stage 
%in% c("1. Beginning")]))*100,1),"% of Beginnings)"),
             ifelse(Stage %in% c("3. Act 3"),paste0(Amount,"<br> (",round((as.numeric(Amount[Stage %in% c("3. Act 3")])/as.numeric(Amount[Stage 
%in% c("2. Act 2")]))*100,1),"% of Act 2 & <br>",round((as.numeric(Amount[Stage 
%in% c("3. Act 3")])/as.numeric(Amount[Stage %in% c("4. Act 4")]))*100,1), " of 
Act 4)"),Amount))))%>%
  mutate(HoverTxt=paste0("The first digit of the amount is 
",paste0(substr(Amount,1,1))))

这允许上面相同的plotly代码产生如下所示的 plot:

在此处输入图像描述

暂无
暂无

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

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