如何通过标记中的标记绘制水平线? [英] How to plot horizontal lines through markers in plotly?

查看:170
本文介绍了如何通过标记中的标记绘制水平线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的一些示例代码来说明我的问题.现在,该图仅生成点.我想做的是有一条水平线穿过每一个点,每边的长度为1. (即对于(2,1)处的一点,我希望线从(1,1)(3,1)).如何在plotly中执行此操作?我在此处进行了查找,但似乎无法弄清楚如何使它工作当y轴不是数字时.

Here is some example code I wrote to illustrate my problem. Right now, the plot generates only the points. What I want to do is have horizontal lines that go through each point, spanning a length of 1 to each side. (i.e. for a point at (2,1) I want the line to run from (1,1) to (3,1)) How can I do this in plotly? I've looked here but can't seem to figure out how to get this to work when the y-axis is not numerical.

library(plotly)

p <- plot_ly(data = mtcars, x = ~mtcars$mpg, y = rownames(mtcars), type = 'scatter', mode = 'markers')
p

编辑 是接受的答案中提供的代码输出(除了显示所有汽车名称).我想知道是否有一种方法可以在每个y轴标签之间绘制一条水平线,例如,在"Volvo 142E"和"Maserati Bora"之间用一条线将它们分开,并沿着图的长度移动.现在,该图的每个水平线都有一个点.我想将这些行中的每一行与另一行分开.

EDIT is the output from the code provided in the accepted answer (except showing all car names). I was wondering if there is a way to draw a horizontal line between each y-axis label, so that for example, between "Volvo 142E" and "Maserati Bora", a line separates them, and goes for the length of the plot. Right now, each horizontal line of the plot has a point on it. I want to separate each of those lines with another line.

推荐答案

要使其正常工作,我们必须使用行索引重新绘制原始散点图,以防止对汽车进行重新排序.然后,我又添加回了轴标签.

To get this to work we had to replot the original scatter plot with an using the row index to prevent plotly from reordering cars. Then I added back to the axis labels.

library(plotly)
##I added the text argument(hover over text) so that I could make sure
##that the yaxis labels matched the points. feel free to delete.
p <- plot_ly(x = mtcars$mpg, y = seq_along(rownames(mtcars)), text=rownames(mtcars),
             type = 'scatter', mode = 'markers')

##This sets some attributes for the yaxis. Note: in your origianl plot every other
##car name was shown on the y-axis so that is what I recreated but you could remove
##the "seq(1,32, by=2)" to show all car names.
ax <- list(
  title = "",
  ticktext = rownames(mtcars)[seq(1,32, by=2)],
  tickvals = seq(1,32, by=2)
)

##This is taken from the plotly help page. y0 and y1 now are set to equal the row
##number and x0 and x1 are +/-1 from the car's mpg    
line <- list(
  type = "line",
  line = list(color = "pink"),
  xref = "x",
  yref = "y"
)

lines <- list()
for (i in seq_along(rownames(mtcars))) {
  line[["x0"]] <- mtcars$mpg[i] - 1
  line[["x1"]] <- mtcars$mpg[i] + 1
  line[c("y0", "y1")] <- i
  lines <- c(lines, list(line))
}

p <- layout(p, title = 'Highlighting with Lines', shapes = lines, yaxis=ax)
p

根据OP的请求进行更新.

要在文本下划线,请使用HTML标记.但是<u> </u>不起作用,因此我们必须使用<span> </span> ...

Update based on OP's request.

To underline text in plotly use HTML tags. But <u> </u> does not work so we have to use <span> </span>...

ax2 <- list(
  title = "", ticktext = paste0('<span style="text-decoration: underline;">', 
                                rownames(mtcars)[1:32 %% 2 ==0],"</span>"),
  tickvals = seq(1,32, by=2), style=list(textDecoration="underline"))

这篇关于如何通过标记中的标记绘制水平线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆