简体   繁体   English

情节中的水平/垂直线

[英]Horizontal/Vertical Line in plotly

I'm using the plotly package and I'm trying to add a horizontal line to a graph.我正在使用plotly包,我正在尝试向图形添加一条水平线。 Is there any way of doing it using plotly?有没有办法使用plotly来做到这一点?

It can be done using ggplot2 and the ggplotly function as shown below:可以使用ggplot2ggplotly函数来完成,如下所示:

library(plotly)

p <- ggplot() +
  geom_hline(yintercept = 4) +
  xlim(c(0,10)) +
  ylim(c(0,10))

ggplotly(p)

But I can't add this to an existing plotly plot.但我无法将其添加到现有的情节情节中。

Also, the axis of my charts are not fixed, so it would be difficult (but not impossible) to just work out an x and y coordinate system for a horizontal line, but I'd rather just add one automatically.此外,我的图表的轴不是固定的,因此很难(但并非不可能)仅计算出水平线的 x 和 y 坐标系,但我宁愿自动添加一个。

I've looked into the y0 and dy arguments, but I can't seem to get the code for those to work, either.我已经研究了 y0 和 dy 参数,但我似乎也无法获得使它们工作的代码。 I'm not quite sure what they do exactly, but I think they're maybe what I'm looking for?我不太确定他们到底在做什么,但我认为他们可能是我正在寻找的东西? I can't find good examples of their usage.我找不到它们用法的好例子。

There are two main ways to do this (using either data or 'paper' coordinates).有两种主要方法可以做到这一点(使用数据或“纸”坐标)。 Assuming data coordinates, the easiest current way is via add_segments() :假设数据坐标,目前最简单的方法是通过add_segments()

plot_ly() %>%
  add_segments(x = 4, xend = 4, y = 0, yend = 10) %>%
  add_segments(x = 3, xend = 5, y = 5, yend = 5)

在此处输入图片说明

Notice how we've hard coded the extent of these lines in data coordinates;请注意我们如何在数据坐标中对这些线的范围进行硬编码; so when zooming and panning the plot, the line will be "clipped" at those values.因此,在缩放和平移绘图时,该线将在这些值处被“剪裁”。 If you don't want these lines to be clipped, use a line shape with xref/yref set to paper (this puts the graph region on a 0-1 scale, rather than on the x/y data scale):如果您不想剪裁这些线,请使用外部参照/yref 设置为纸的线条形状(这会将图形区域置于 0-1 比例,而不是 x/y 数据比例):

vline <- function(x = 0, color = "red") {
  list(
    type = "line", 
    y0 = 0, 
    y1 = 1, 
    yref = "paper",
    x0 = x, 
    x1 = x, 
    line = list(color = color)
  )
}

hline <- function(y = 0, color = "blue") {
  list(
    type = "line", 
    x0 = 0, 
    x1 = 1, 
    xref = "paper",
    y0 = y, 
    y1 = y, 
    line = list(color = color)
  )
}

plot_ly() %>%
  layout(shapes = list(vline(4), hline(5)))

在此处输入图片说明

Alternatively, you could add a shape (ie line) under layout().或者,您可以在 layout() 下添加一个形状(即线条)。 The following example adds a vertical line:以下示例添加了一条垂直线:

p <- plot_ly(data, x = ~x.data, y = ~y.data, text = ~text.data, type = 'scatter', 
       mode = 'markers', marker = list(size = ~size.data, opacity= 0.5)) %>%
     layout(shapes=list(type='line', x0= 0.2, x1= 0.2, y0=min(allyvalues), y1=max(allyvalues), line=list(dash='dot', width=1)),
       title = 'This is the Title',
       xaxis = list(title = "X-Axis", showgrid = TRUE),
       yaxis = list(title = "Y-Axis", showgrid = TRUE))
p

Building on Carson's nice answer above , here is a convenience function closer to ggplot's geom_vline()基于上面卡森的好答案,这里有一个更接近 ggplot 的geom_vline()的便利函数

# Add vertical line(s) at position x to plotly plot p
# Additional arguments: color, width (px), dash ('solid','dot', 'dash', etc)
# See https://plotly.com/r/reference/#layout-shapes-items-shape-line
add_vline = function(p, x, ...) {
  l_shape = list(
    type = "line", 
    y0 = 0, y1 = 1, yref = "paper", # i.e. y as a proportion of visible region
    x0 = x, x1 = x, 
    line = list(...)
  )
  p %>% layout(shapes=list(l_shape))
}

To make the function additive the following modifications to the function can be used为了使函数可加,可以对函数进行以下修改


add_vline = function(p, x, ...) {

  if(!is.null(p$x$layoutAttrs)){
      index <- unname(which(sapply(p$x$layoutAttrs, function(x) 
  !is.null(x$shapes))))
    } else {
      index <- integer()
    }

    l_shape = list(
      type = "line",
      y0 = 0, y1 = 1, yref = "paper", # i.e. y as a proportion of visible region
      x0 = x, x1 = x,
      line = list(
        ...
      ),
      layer = "below"
    )

    if(length(index) > 0){
      shapes <- p$x$layoutAttrs[[index]]$shapes
      shapes[[length(shapes) + 1]] <- l_shape
      p$x$layoutAttrs[[index]]$shapes <- shapes
    } else {
      p <- plotly::layout(
        p = p,
        shapes = list(l_shape)
      )
    }
   p
}


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

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