我试图在绘图中在同一图表上绘制多条线。问题在于正在绘制的每个变量,从图中创建新的y轴值集。这可以解决吗?我希望我创建的所有线图具有相同的y轴。以下是代码和生成的图。
p1 <- plot_ly(data = st_data, x = ~Date) %>% add_lines(y = ~Close,name =
"Close") %>%
add_lines(y=~Bollinger,name="Bollinger")
在图中,y轴的值范围为61.85至65.90,再次为62.15至65.49。
理想情况下,我正在寻找介于61.85和65.90之间的y轴值,并将两条线绘制在同一轴上。
Adding the input data:
Date Close Bollinger
1/30/2015 9:34 65.55 NA
1/30/2015 9:34 65.43 NA
1/30/2015 9:35 65.52 NA
1/30/2015 9:35 65.37 NA
1/30/2015 9:36 65.68 65.184
1/30/2015 9:36 65.4 65.303
1/30/2015 9:36 65.51 65.4155
1/30/2015 9:36 65.8 65.499
1/30/2015 9:36 65.6 65.548
答案 0 :(得分:0)
是的,您的代码应该已经有效了。我认为sjakw是正确的,因为你有一些其他代码会产生问题。尝试打开一个新脚本,然后粘贴以下代码。你应该得到一个单轴y轴。
library(data.table)
library(plotly)
st_data <- fread('Date , Close, Bollinger
1/30/2015 9:34, 65.55, NA
1/30/2015 9:34, 65.43, NA
1/30/2015 9:35, 65.52, NA
1/30/2015 9:35, 65.37, NA
1/30/2015 9:36, 65.68, 65.184
1/30/2015 9:36, 65.4 , 65.303
1/30/2015 9:36, 65.51, 65.4155
1/30/2015 9:36, 65.8 , 65.499
1/30/2015 9:36, 65.6 , 65.548 ')
p1 <- plot_ly(data = st_data, x = ~Date) %>% add_lines(y = ~Close,name = "Close") %>%
add_lines(y=~Bollinger,name="Bollinger")
p1
我更喜欢以下方法。
p2 <- plot_ly()
p2 <- add_lines(p, data = st_data, x = ~Date, y = ~Close, name = "Close")
p2 <- add_lines(p, data = st_data, x = ~Date, y = ~Bollinger, name = "Bollinger")
p2
您的数据在&#34;范围内&#34;格式。如果您将数据融入&#34; long&#34;您可以将相似的代码用于R Plotly Book。格式:
st_data_long <- melt.data.table(st_data, id = "Date", measure.vars = c("Close", "Bollinger"),
value.factor = TRUE, variable.name = "PriceType", value.name = "Price")
p3 <- plot_ly(st_data_long, x = ~Date, y = ~Price) %>%
add_lines(color = ~PriceType)
p3
我还尝试使用R:
中包含的样本数据集# First make Date one column
airquality <- data.table(airquality)
airquality[, Date := do.call(paste, .SD), .SDcols = c("Month", "Day")]
p4 <- plot_ly()
p4 <- add_lines(p1, data = airquality, x = ~Date, y = ~Ozone, name = "Ozone")
p4 <- add_lines(p1, data = airquality, x = ~Date, y = ~Temp, name = "Temp")
p4