R:在绘图中格式化轴和标题(时间序列) [英] R: formatting axis and titles on plotly plots (time series)

查看:83
本文介绍了R:在绘图中格式化轴和标题(时间序列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R编程语言.使用以下教程:

但是,我无法在此图中添加自定义标题和轴:

  with_titles =数据%>%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,颜色=〜系数(类))with_titles错误:$运算符对原子向量无效 

尝试更改此图的x轴时也会产生类似的错误:

  with_titles_and_axis =数据%>%group_by(class)%&%;%plot_ly(x =〜yday(date_decision_made))%>%layout(title ='Damage vs time',xaxis = xaxis = list(type ="date",tickformat ='%d/%m/%Y',showticklabels ='True',tick0 ="2014-01-01",dtick ="M12"),yaxis =损坏")%>%add_lines(y =〜property_damages_in_dollars,颜色=〜系数(类))#source:https://community.plotly.com/t/plotly-time-series-forecasting-modify-default-x-axis-and-y-axis-range/11390/2 

有人可以告诉我我做错了什么吗?是否还可以在悬停文字上显示日期?谢谢

解决方案

我刚刚想出了如何将日期更改为更标准的格式:

  a =数据%>%group_by(class)%&%;%plot_ly(x =〜(date_decision_made))%&%;%add_lines(y =〜property_damages_in_dollars,颜色=〜系数(类)) 

现在,我必须弄清楚如何更改标题/轴,以及如何使日期显示在悬停文本上

I am using the R programming language. 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? I was able to make an interactive time series 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)
    ) 

However, I am having trouble adding custom titles and axis to this graph:

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

A similar error is produced when trying to change the x-axis of this graph :

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

Can someone please tell me what I am doing wrong? Is it also possible to display the date over the hover text? Thanks

解决方案

I just figured out how to change the date to a more standard format:

a = data  %>% 
    
    group_by(class) %>% 
    plot_ly(x = ~ (date_decision_made)) %>% 
    add_lines(y = ~ property_damages_in_dollars, 
              color = ~ factor(class)
    ) 

Now, I have to figure out how to change the titles/axis, as well as make the date appear during the hover text

这篇关于R:在绘图中格式化轴和标题(时间序列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆