Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 绘图仪中的水平/垂直线_R_Plotly - Fatal编程技术网

R 绘图仪中的水平/垂直线

R 绘图仪中的水平/垂直线,r,plotly,R,Plotly,我正在使用plotly包,并试图在图形中添加一条水平线。有没有办法用plotly来做呢 可以使用ggplot2和ggplotly功能完成此操作,如下所示: library(plotly) p <- ggplot() + geom_hline(yintercept = 4) + xlim(c(0,10)) + ylim(c(0,10)) ggplotly(p) library(plotly) p有两种主要方法(使用数据或“纸”坐标)。假设数据坐标,当前最简单的方法是通过添加

我正在使用
plotly
包,并试图在图形中添加一条水平线。有没有办法用plotly来做呢

可以使用
ggplot2
ggplotly
功能完成此操作,如下所示:

library(plotly)

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

ggplotly(p)
library(plotly)

p有两种主要方法(使用数据或“纸”坐标)。假设数据坐标,当前最简单的方法是通过
添加_段()

注意我们如何在数据坐标中硬编码这些线的范围;因此,当缩放和平移绘图时,线条将在这些值处“剪裁”。如果不希望剪裁这些线,请使用将xref/yref设置为paper的图形区域(这会将图形区域置于0-1的比例上,而不是x/y数据比例上):


vline或者,您可以在layout()下添加一个形状(即线)。以下示例添加了一条垂直线:

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
p%
布局(形状=列表(类型='line',x0=0.2,x1=0.2,y0=最小值(所有值),y1=最大值(所有值),线=列表(虚线='dot',宽度=1)),
标题='这是标题',
xaxis=list(title=“X轴”,showgrid=TRUE),
yaxis=列表(title=“Y轴”,showgrid=TRUE))
P
在Carson的基础上,这里有一个更接近ggplot的便利功能
geom_vline()


要使函数增加,可以对函数进行以下修改


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
}



加法=函数(p,x,…){
如果(!为.null(p$x$layouttrs)){

索引谢谢。我如何避免在图例中显示添加的垂直线?出于某种原因,您答案中的第一个代码不会同时生成两行。它只生成水平线。但如果我这样更改:
plot_ly()%%>%add_line(x=c(4,4),y=c(0,10))%%>%add_line(x=c(3,5),y=c(5,5))
,它生成两行。我有
绘图版
4.7.0
如果我们有双y轴,并且想要向hlines显示什么是“AllyValue”,我们如何使用它?我如何获得它们?我不能以这种方式添加多行:
fig%>%add\u vline(…)%>%add\u vline(…)
因为我认为多个
layout
调用并不是累加的。一个小小的“gotcha”吸引了我。对于多行,构造一个列表并传递到
layout()中的
形状。
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
# 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))
}

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
}