【问题标题】:R Plotly - Add annotation to reference line in a scatter plotR Plotly - 在散点图中向参考线添加注释
【发布时间】: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


    【解决方案1】:

    以下代码解决了我的问题。希望它可以帮助某人。

    library(dplyr)
    library(plotly)
    
    iris <- datasets::iris
    
    scatter <- 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))), 
                                      annotations = list(list( 
                                                x = 3.4,
                                                y = 7.9,
                                                xref = 'x',
                                                yref = 'y',
                                                text = 'Mean Sepal Width',
                                                showarrow = FALSE
                                              ),
                                                list( 
                                                  x = 4.7,
                                                  y = 5.6,
                                                  xref = 'x',
                                                  yref = 'y',
                                                  text = 'Mean Sepal Length',
                                                  showarrow = FALSE
                                                )))      
    scatter
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 2022-01-02
      • 2017-01-17
      • 1970-01-01
      相关资源
      最近更新 更多