【发布时间】:2019-01-27 02:53:58
【问题描述】:
我使用 iris 数据集在 R 中使用 Plotly 创建了一个散点图,并添加了两条参考线,一条在 x 轴上表示平均 Sepal.Width,另一条在 y 轴上表示平均 Sepal.Length。
下面是可执行的R代码:
library(dplyr)
library(plotly)
iris <- datasets::iris
scatterplot <- plot_ly(data = iris, x = ~Sepal.Width, y = ~Sepal.Length, type = 'scatter',
text = ~Species,
color = I('orange')) %>%
layout(shapes=list(list(type = 'line',
x0 = mean(iris$Sepal.Width),
x1 = mean(iris$Sepal.Width),
y0 = 4,
y1 = 8,
line = list(width = 2)),
list(type = 'line',
x0 = 1.5,
x1 = 5,
y0 = mean(iris$Sepal.Length),
y1 = mean(iris$Sepal.Length),
line = list(width = 2))))
scatterplot
上面的 R 代码产生以下内容 Plotly output
我想为两条参考线(“Mean Sepal Width”和“Mean Sepal Length”)添加注释文本(即标签)。
我遇到了similar post,但是那里提到的解决方案对我不起作用。如果有人可以为我提供代码解决方案,那将不胜感激。
PS:我使用的是 Plotly 4.8.0 版
【问题讨论】:
标签: r plotly scatter-plot scatter r-plotly