简体   繁体   English

R Plotly-向散点图中的参考线添加注释

[英]R Plotly - Add annotation to reference line in a scatter plot

I have created a scatterplot with Plotly in R using the iris dataset and have added two reference lines, one on the x-axis denoting the mean Sepal.Width and the other on the y-axis denoting the mean Sepal.Length. 我使用虹膜数据集在R中使用Plotly创建了一个散点图,并添加了两条参考线,一条在x轴上表示平均值Sepal.Width,另一条在y轴上表示平均值Sepal.Length。

Below is the executable R code: 以下是可执行的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

The above R code produces the following Plotly output 上面的R代码产生以下Plotly输出

I want to add annotation text (ie label) for the two reference lines ("Mean Sepal Width" and "Mean Sepal Length"). 我想为两条参考线(“均值分隔宽度”和“均值分隔长度”)添加注释文本(即标签)。

I came across a similar post however the solution mentioned there did not work for me. 我遇到过类似的帖子,但是那里提到的解决方案对我不起作用。 If someone can provide me the solution with the code then that would be highly appreciated. 如果有人可以向我提供代码的解决方案,那么将不胜感激。

PS: I am using Plotly version 4.8.0 PS:我正在使用Plotly版本4.8.0

The following code solved my problem. 以下代码解决了我的问题。 Hope it helps someone. 希望它可以帮助某人。

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

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

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