R语言 用plotly制作点网格线

toiithl6  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(126)

阅读plotly的文档时,我想我可以使用griddash来设计网格线的样式,使其不是实线而是虚线(比如说)。
但是,这并不起作用(gridcolorgridwidth确实起作用,这里只是为了更好的可见性):

library(plotly)

d <- data.frame(x = LETTERS[1:3], y = c(13, 50, 97))

plot_ly(d) %>%
  add_trace(x = ~ x, y = ~ y, type = "bar") %>%
  layout(yaxis = list(griddash = "dot", gridwidth = 2, gridcolor = "red"))

手动添加stroke-dasharray就可以了:

这让我相信这是一个错误,应该这样归档?

mjqavswn

mjqavswn1#

这个参数(以及其他几个参数)不起作用的原因是Plotly JS依赖。现在R包似乎仍然在使用2.11.1。与此同时,Plotly JS在2.22上?两点二三?
如果您希望能够使用这个参数,这是使plotly为您所用的一种方法--更改依赖项。
顺便说一句,我在这里使用了2.21,因为我已经经常使用这个块了。您始终可以捕获较新版本的依赖项URL。

library(htmltools)
library(plotly)

#----------- your code ------------
d <- data.frame(x = LETTERS[1:3], y = c(13, 50, 97))

plot_ly(d) %>%
  add_trace(x = ~ x, y = ~ y, type = "bar") %>%
  layout(yaxis = list(griddash = "dot", gridwidth = 2, gridcolor = "red"))

#----------- added code -------------

# assign plot to object
plt <- plotly::last_plot()   # !! function exists in multiple libraries

# create new Plotly JS dependency
newDep <- htmlDependency(name = "plotly-latest", 
                         version = "2.21.1",     
                         src = list(href = "https://cdn.plot.ly"),
                         script = "plotly-2.21.0.min.js")

# add that dependency to plot
plt$dependencies[[6]] <- newDep

# see what you've got
plt

相关问题