Plotly无法正确渲染图表。

huangapple go评论68阅读模式
英文:

Plotly doesn't render the plot correctly

问题

我尝试使用buraR包中的trace_explorer函数创建的图表,使用ggplotly函数使其具有交互性,但结果的图表不符合预期。

这是代码:

library(ggplot2)
library(bupaR)

patients <- eventdataR::patients # bupaR数据集

df <- eventlog(patients,
               case_id = "patient",
               activity_id = "handling",
               activity_instance_id = "handling_id",
               lifecycle_id = "registration_type",
               timestamp = "time",
               resource_id = "employee")

tr <- df %>% processmapR::trace_explorer(type = "frequent", coverage = 1.0)

# tr # 打印ggplot以查看预期输出!
ggplotly(tr)

以及生成的图表:

Plotly无法正确渲染图表。

我尝试在ggplot2中使用主题选项,然后使用layout函数,但结果仍然没有图例。

ggtrace <- trace_explorer(df,
                          type = "frequent", 
                          coverage = 1.0)

ggtrace <- ggtrace + 
  theme(legend.position="none") +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()
  )

plotly_trace <- ggplotly(ggtrace)

layout(plotly_trace, 
                    margin=list(l=50, b=50), 
                    legend=list(x=1.05)
)

Plotly无法正确渲染图表。

预期的结果应该与原始的ggplot类似,但具有来自plotly的交互选项。

Plotly无法正确渲染图表。

Plotly无法正确渲染图表。

注意:我正在使用软件包的先前版本,并希望保持这些版本。

> packageVersion('ggplot2')
[1] ‘3.3.0> packageVersion('bupaR')
[1] ‘0.5.2
英文:

I am trying to make a plot created with the trace_explorer function from buraR package interactive using the ggplotly function but the resulting plot is not the expected.

Here is the code :

library(ggplot2)
library(bupaR)

patients &lt;- eventdataR::patients # dataset from bupaR

df &lt;- eventlog(patients,
               case_id = &quot;patient&quot;,
               activity_id = &quot;handling&quot;,
               activity_instance_id = &quot;handling_id&quot;,
               lifecycle_id = &quot;registration_type&quot;,
               timestamp = &quot;time&quot;,
               resource_id = &quot;employee&quot;)




tr &lt;- df %&gt;% processmapR::trace_explorer(type = &quot;frequent&quot;, coverage = 1.0)

# tr # print the ggplot to see the expected output!
ggplotly(tr)

and the resulting plot

Plotly无法正确渲染图表。

I tried to use the theme option in ggplot2 and then the layout function but the result is still the same without the legend.

ggtrace &lt;- trace_explorer(df,
                          type = &quot;frequent&quot;, 
                          coverage = 1.0)

ggtrace &lt;- ggtrace + 
  theme (legend.position=&quot;none&quot;) +
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()
  )

plotly_trace &lt;- ggplotly(ggtrace)

layout(plotly_trace, 
                    margin=list(l=50, b=50), 
                    legend=list(x=1.05)
)

Plotly无法正确渲染图表。

The expected out should be a like the original ggplot but with interactive options coming from plotly.

Plotly无法正确渲染图表。

Plotly无法正确渲染图表。

Note I am using previous version of the packages and want to keep these versions.

&gt; packageVersion(&#39;ggplot2&#39;)
[1] ‘3.3.0’
&gt; packageVersion(&#39;bupaR&#39;)
[1] ‘0.5.2’

答案1

得分: 1

这是一个关于从 ggplot 转换到 plotly 的代码段,包括对注释、形状和y轴网格的处理。

如果你有任何进一步的问题或需要帮助,请随时告诉我。

英文:

It's been a while since you asked, but I just came across your question. Lots of odd things happened in the conversion from ggplot to plotly here.

I have written a UDF to address the 3 primary issues that are presented: annotations, shapes, and the y-axis grid.

The annotations:

This is the text in the gray boxes on the right side. In the ggplotly object, this became &quot;xx.x%&lt;br /&gt;xxx&lt;br /&gt;xx.x%&quot; which is odd! The first two annotations are actually the axis titles, which is also a bit strange. I left the axis labels as is. The remaining annotations are those found in the gray boxes. That's why you'll see from 3:length(plt$x$layout$annotations) in the first call for lapply. Within these fixes, I obtain each annotation, split the string of values (&quot;xx.x%&lt;br /&gt;xxx&lt;br /&gt;xx.x%&quot;), and create an annotation for each value. I changed the text to be centered, instead of left justified. Lastly, I change the text position to increment based on the number of strings (so that the numbers are not on top of each other).

The shapes:

These are the gray backgrounds of the shapes and the rectangles around each horizontal row. The shapes alternate: rectangle, gray block, rectangle and so on. So to capture the shapes that are necessary for modification, the if function looks for even index values between one and the number of shapes in the plot. If even, the shape size is changed to a dynamic, instead of fixed size (so it grows or shrinks if your plot does), the text is centered, and the start positions are increased between the 3 blocks so they aren't on top of each other. Lastly, the widths are set to .19 (19% of the plot size) from their start position. (This .19 is selected due to the .20 I used to increment the block positions by, leaving .1 of which is the white line between blocks.)

The grid for the y-axis:

The reason you have this mess of lines about each of the rows of data is that the y-axis grid is used, whether you designate this or not in ggplot. So the last step of the UDF is to change each y-axis (one for each row in your plot) to showgrid = F.

This uses the object plotly_trace as presented in your question. (The layout arguments you provided aren't utilized.) There are comments within the code to help explain what is happening. It could be consolidated a bit more, but I think it might be easier to follow as it's written.

I've included your code as it was used in my answer.

library(tidyverse)
library(bupaR)
library(plotly)

patients &lt;- eventdataR::patients # dataset from bupaR

df &lt;- eventlog(patients,
               case_id = &quot;patient&quot;,
               activity_id = &quot;handling&quot;,
               activity_instance_id = &quot;handling_id&quot;,
               lifecycle_id = &quot;registration_type&quot;,
               timestamp = &quot;time&quot;,
               resource_id = &quot;employee&quot;)

tr &lt;- df %&gt;% processmapR::trace_explorer(type = &quot;frequent&quot;, coverage = 1.0)

# tr # print the ggplot to see the expected output!
ggplotly(tr)

ggtrace &lt;- trace_explorer(df,
                          type = &quot;frequent&quot;, 
                          coverage = 1.0)

(ggtrace &lt;- ggtrace + 
  theme (legend.position=&quot;none&quot;) +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()
  ))

plotly_trace &lt;- ggplotly(ggtrace)

buParFix &lt;- function(plt) {
  # annotations 1 &amp; 2 are actually the axis labels (odd)
  repAnn &lt;- invisible(lapply(3:length(plt$x$layout$annotations), function(k) {
    # get text, create separate annot for each text
    tx &lt;- plt$x$layout$annotations[[k]]$text
    trs &lt;- strsplit(tx, &quot;&lt;br /&gt;&quot;)[[1]] %&gt;% imap(., function(i, j) {
      otr &lt;- plt$x$layout$annotations[[k]]         # collect &amp; copy annot
      otr$text &lt;- i
      otr$xanchor &lt;- &quot;center&quot;                      # horizontal alignment
      otr$x &lt;- 1.1 + ((j - 1) * .2)                # move to the right each iter
      otr
    }) 
    trs
  })) %&gt;% unlist(recursive = F)                    # remove one list of lists level
                                                   # fix text in end caps
  plt$x$layout$annotations &lt;- append(plt$x$layout$annotations[1:2], repAnn)  
  
  shps &lt;- invisible(lapply(1:length(plotly_trace$x$layout$shapes), function(q) {
    if(q %% 2 == 0) {
      tr &lt;- plotly_trace$x$layout$shapes[[q]]      # collect &amp; copy shape
      tr$xsizemode = &quot;scaled&quot;                      # make width dynamic
      tr$xref = &quot;paper&quot;                            # use paper space
      trts &lt;- map(1:3, function(k) {               # three columns to the right
        tr$x0 &lt;- 1 + ((k - 1) * .2)                # set x0 and x1
        tr$x1 &lt;- tr$x0 + .19                       # move to the right each iter
        tr
      })
      return(append(plotly_trace$x$layout$shapes[q - 1], trts)) # return updated shapes
    }
  })) %&gt;% unlist(recursive = F)                    # remove one list of lists level
  
  plt$x$layout$shapes &lt;- shps                      # fix gray background end caps
  
  # fix grid for y-axes
  ys &lt;- length(which(startsWith(names(plt$x$layout), &quot;yaxi&quot;))) # count of y-axes
  lapply(paste0(&quot;yaxis&quot;, c(&quot;&quot;, 2:ys)), function(i) {     # hide grid for each
    plt$x$layout[[i]]$showgrid &lt;&lt;- F
  })
  plt %&gt;% layout(margin = list(r = 200))                 # return modified plot
}
buParFix(plotly_trace)

Plotly无法正确渲染图表。

huangapple
  • 本文由 发表于 2023年2月6日 21:38:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定