[英]R: formatting axis and titles on plotly plots (time series)
我正在使用 R 編程語言。 Using the following tutorial: https://plotly.com/r/time-series/ and this stackoverflow question: How to plot multiple series/lines in a time series using plotly in R? 我能夠制作一個交互式時間序列 plot:
library(xts)
library(ggplot2)
library(dplyr)
library(plotly)
library(lubridate)
#time series 1
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
property_damages_in_dollars <- rnorm(731,100,10)
final_data <- data.frame(date_decision_made, property_damages_in_dollars)
final_data %>%
mutate(date_decision_made = as.Date(date_decision_made)) %>%
add_count(week = format(date_decision_made, "%W-%y"))
final_data$class = "time_series_1"
#time series 2
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")
property_damages_in_dollars <- rnorm(731,10,10)
final_data_2 <- data.frame(date_decision_made, property_damages_in_dollars)
final_data_2 %>%
mutate(date_decision_made = as.Date(date_decision_made)) %>%
add_count(week = format(date_decision_made, "%W-%y"))
final_data_2$class = "time_series_2"
#combine
data = rbind(final_data, final_data_2)
data$class = as.factor(data$class)
a = data %>%
group_by(class) %>%
plot_ly(x = ~ yday(date_decision_made)) %>%
add_lines(y = ~ property_damages_in_dollars,
color = ~ factor(class)
)
但是,我無法在此圖中添加自定義標題和軸:
with_titles = data %>%
group_by(class) %>%
plot_ly(x = ~ yday(date_decision_made)) %>% layout(title = 'damages vs time', xaxis = 'time', yaxis = 'damage') %>%
add_lines(y = ~ property_damages_in_dollars,
color = ~ factor(class)
)
with_titles
Error: $ operator is invalid for atomic vectors
嘗試更改此圖的 x 軸時會產生類似的錯誤:
with_titles_and_axis = data %>%
group_by(class) %>%
plot_ly(x = ~ yday(date_decision_made)) %>% layout(title = 'damages vs time', xaxis = xaxis =list(type="date",
tickformat='%d/%m/%Y',
showticklabels='True',
tick0= "2014-01-01",
dtick= "M12"), yaxis = 'damage') %>%
add_lines(y = ~ property_damages_in_dollars,
color = ~ factor(class)
)
#source: https://community.plotly.com/t/plotly-time-series-forecasting-modify-default-x-axis-and-y-axis-range/11390/2
有人可以告訴我我做錯了什么嗎? 是否也可以在 hover 文本上顯示日期? 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.