简体   繁体   English

如何将单独的数据添加到 R 中的绘图中

[英]How to add separate data to plotly graph in R

I build monthly reports where I plot web traffic on a plotly line graph, and when I hover over the data points, it pulls up the date & page visits.我构建月度报告,在其中绘制线图上的网络流量,当我将鼠标悬停在数据点上时,它会显示日期和页面访问量。 I have a second graph where I have the date and the article that we posted the date.我有第二张图表,其中包含日期和发布日期的文章。 An example is below:一个例子如下:

library(plotly)

Day <- seq.Date(as.Date("2020-08-01"),as.Date("2020-08-31"), by=1)
Visits <- round(rnorm(31, 150, 5),0)
Web_Traffic <- data.frame(Day, Visits)

Publishing_Date <- as.Date(c("2020-08-01", "2020-08-15", "2020-08-24"))
Article <- c("Article 1", "Article2", "Article3")

Blog <- data.frame(Publishing_Date, Article)

plot_ly(Web_Traffic, x=Day, y=Visits, type = "scatter", mode = "line")

Is there a way where on Aug 1, Aug 15, and Aug 24, on the line graph, when I hover over the data points, it can also include the article that was published on that day?有没有办法在 8 月 1 日、8 月 15 日和 8 月 24 日,在折线图上,当我将鼠标悬停在数据点上时,它还可以包含当天发表的文章? and if this is possible, is there a way to keep the data up on the graph without needing to hover over the graph?如果这是可能的,有没有办法将数据保持在图表上,而无需将鼠标悬停在图表上? (ie if I want to copy the graph to a powerpoint) (即,如果我想将图形复制到幻灯片中)

You could do something like the following - where you mark the published articles with vertical segments:您可以执行以下操作 - 使用垂直段标记已发布的文章:

plot_ly() %>%
  add_lines(data=Web_Traffic, x=Day, y=Visits, name="Visits" ) %>%
  add_segments(data=Blog, x=Publishing_Date, 
                          xend=Publishing_Date, 
                          y=min(Web_Traffic$Visits), 
                          yend=max(Web_Traffic$Visits), 
                          name="Published Articles")

在此处输入图片说明

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

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