简体   繁体   English

如何将 add_trace 添加到 R 中从 ggplotly 创建的 plotly object

[英]How to add_trace to a plotly object created from ggplotly in R

I want to be able to add a new line/trace to a plotly object. This plotly object will be created from a ggplot object using ggplotly.我希望能够向 plotly object 添加新的线/轨迹。这个 plotly object 将使用 ggplotly 从 ggplot object 创建。

I tried 1. creating the plotly object and 2. using add_trace to the plotly object, but I am unable to generate the chart.我试过 1. 创建 plotly object 和 2. 使用 add_trace 到 plotly object,但我无法生成图表。

I have used ggplot2 for a while now but am new to plotly/ggplotly.我已经使用 ggplot2 一段时间了,但对 plotly/ggplotly 还是个新手。

Any help on this is appreciated please.请对此提供任何帮助。

library(ggplot2)
library(plotly)

structure(list(Date = structure(c(1646697600, 1646784000, 1646870400, 
1646956800, 1647216000, 1647302400, 1647388800, 1647475200, 1647561600, 
1647820800, 1647907200, 1647993600, 1648080000, 1648166400, 1648425600, 
1648512000, 1648598400, 1648684800, 1648771200, 1649030400, 1649116800, 
1649203200, 1649289600, 1649376000, 1649635200, 1649721600, 1649808000, 
1649894400, 1649980800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    PolicyRate = c(0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 
    0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 
    0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 0.375, 
    0.375, 0.375, 0.375, 0.375, 0.375), USGG1y = c(1.0522, 1.096, 
    1.1063, 1.0907, 1.2092, 1.1937, 1.1989, 1.1911, 1.1755, 1.2477, 
    1.3147, 1.4839, 1.5409, 1.629, 1.6627, 1.647, 1.5951, 1.595, 
    1.6649, 1.6675, 1.7168, 1.7245, 1.6933, 1.7347, 1.7528, 1.6567, 
    1.6566, 1.6979, 1.6979), USGG2y = c(1.601, 1.682, 1.698, 
    1.75, 1.863, 1.852, 1.941, 1.917, 1.939, 2.118, 2.167, 2.099, 
    2.141, 2.273, 2.331, 2.368, 2.309, 2.337, 2.459, 2.424, 2.517, 
    2.473, 2.463, 2.516, 2.502, 2.407, 2.351, 2.456, 2.456), 
    USGG5y = c(1.778, 1.88, 1.925, 1.948, 2.092, 2.107, 2.182, 
    2.145, 2.144, 2.323, 2.4, 2.32, 2.399, 2.546, 2.558, 2.5, 
    2.44, 2.462, 2.56, 2.552, 2.697, 2.683, 2.709, 2.756, 2.791, 
    2.69, 2.654, 2.788, 2.788), USGG10y = c(1.848, 1.955, 1.99, 
    1.995, 2.135, 2.146, 2.188, 2.173, 2.152, 2.292, 2.384, 2.294, 
    2.374, 2.477, 2.461, 2.397, 2.352, 2.341, 2.385, 2.398, 2.549, 
    2.6, 2.66, 2.705, 2.782, 2.724, 2.702, 2.829, 2.829), USGG30y = c(2.229, 
    2.337, 2.369, 2.356, 2.473, 2.479, 2.455, 2.471, 2.423, 2.519, 
    2.603, 2.486, 2.541, 2.587, 2.542, 2.503, 2.477, 2.45, 2.434, 
    2.458, 2.574, 2.628, 2.681, 2.72, 2.811, 2.813, 2.814, 2.918, 
    2.918)), row.names = c(NA, -29L), class = "data.frame")

g <- data %>% ggplot() +
     geom_line(aes(x=Date, y=USGG2y))

g
p <- ggplotly(g)
p # works fine until here.

p1 <- p %>% add_trace(x=~Date, y=~USGG5y,yaxis = "y2", mode="lines+markers",type = "scatter")
#doesnt throw up any obvious error"

p1 #this one gives me a blank plotly chart"

This one took a while.这花了一段时间。

Plotly's interpretation (or ggplot2 's delivery) was rendered as if the dates were character fields; Plotly 的解释(或ggplot2的传递)被渲染为好像日期是字符字段; the axis tick labels were literal labels.轴刻度标签是文字标签。 The plot wouldn't render because it presented more of a picture of what ggplot2 created than a graph. plot 不会呈现,因为它呈现的更多是ggplot2创建的图片而不是图表。

The way forward前进的道路

  • change the x data for the trace to date type data将跟踪的x数据更改为日期类型数据
  • update the axes to hold values instead of labels更新轴以保存值而不是标签
  • add the new trace添加新的跟踪

There are a few different ways that would work to accomplish this.有几种不同的方法可以实现这一点。 I've only provided one method.我只提供了一种方法。

After creating your ggplotly object, update the object. The easiest way to do this is by adding the exact same data from your data frame.创建ggplotly object 后,更新 object。最简单的方法是从数据框中添加完全相同的数据。

p = ggplotly(g)
p[["x"]][["data"]][[1]][["x"]] <- data$Date # change the 'type'

With the axes, you could update the object or assign new settings.使用轴,您可以更新 object 或分配新设置。 This method assigns some new settings.此方法分配一些新设置。 Lastly, add the new trace.最后,添加新的轨迹。

p %>% layout(yaxis = list(range = c(0, 3), tickmode = "auto"),
             xaxis = list(range =  c(data$Date[1], data$Date[nrow(data)]),
                          tickformat = "%b %d", type = "date", 
                          tickmode = "auto")) %>% 
  add_lines(x = ~Date, y = ~USGG5y)

That's it.就是这样。 A few notes on what I did here first...关于我首先在这里所做的一些注意事项......

  • range sets the lower and upper limit range设置下限和上限
  • tickmode has three options tickmode有三个选项
    • it is set to array when you use labels当你使用标签时它被设置为array
    • I changed it to auto (the labels are ignored)我将其更改为自动(标签被忽略)
  • for the x-axis, that range equates to the min/max dates in date type format对于 x 轴,该range等于日期类型格式的最小/最大日期
  • tickformat : tickformat
    • %b is d3 coding for the abbreviated month name %b是缩写月份名称的 d3 编码
    • %d is the day of the month without padding (ie, Mar 1 versus Mar 01 for March 1st.) %d是一个月中没有填充的日期(即,3 月 1 日的 Mar 1 与 Mar 01。)
    • there's a space between the two, so that there's a space between the month and day on the axis两者之间有一个空格,这样轴上的月和日之间就有一个空格

在此处输入图像描述

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

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