用PLOTLY绘制图 [英] Plot graph with PLOTLY

查看:124
本文介绍了用PLOTLY绘制图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据集的一个小例子.该数据集包含每周约52周的数据.您可以通过以下代码查看数据:

This is small example of my data set.This set contain weekly data about 52 weeks.You can see data with code below:

# CODE
 #Data

    ARTIFICIALDATA<-dput(structure(list(week = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
    13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
    29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
    45, 46, 47, 48, 49, 50, 51, 52), `2019 Series_1` = c(534.771929824561, 
    350.385964912281, 644.736842105263, 366.561403508772, 455.649122807018, 
    533.614035087719, 829.964912280702, 466.035087719298, 304.421052631579, 
    549.473684210526, 649.719298245614, 537.964912280702, 484.982456140351, 
    785.929824561404, 576.736842105263, 685.508771929824, 514.842105263158, 
    464.491228070175, 608.245614035088, 756.701754385965, 431.859649122807, 
    524.315789473684, 739.40350877193, 604.736842105263, 669.684210526316, 
    570.491228070175, 641.649122807018, 649.298245614035, 664.210526315789, 
    530.385964912281, 754.315789473684, 646.80701754386, 764.070175438596, 
    421.333333333333, 470.842105263158, 774.245614035088, 752.842105263158, 
    575.368421052632, 538.315789473684, 735.578947368421, 522, 862.561403508772, 
    496.526315789474, 710.631578947368, 584.456140350877, 843.19298245614, 
    563.473684210526, 568.456140350877, 625.368421052632, 768.912280701754, 
    679.824561403509, 642.526315789474), `2020 Series_1` = c(294.350877192983, 
    239.824561403509, 709.614035087719, 569.824561403509, 489.438596491228, 
    561.964912280702, 808.456140350877, 545.157894736842, 589.649122807018, 
    500.877192982456, 584.421052631579, 524.771929824561, 367.438596491228, 
    275.228070175439, 166.736842105263, 58.2456140350878, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA)), row.names = c(NA, -52L), class = c("tbl_df", "tbl", 
    "data.frame")))


  # CODE WITH PLOTLY  
library(tidyverse)
library(plotly)
library(reshape2)
library(ggplot2)
library(dplyr)
ARTIFICIALDATA_rec <- ARTIFICIALDATA %>% 
  gather(key = Year_indicator, value = time_series_value, -1)


    ARTIFICIALDATA_rec$color <- factor(ARTIFICIALDATA_rec$Year_indicator, labels = c("royalblue", "orange")) 

    Chart <- plot_ly(ARTIFICIALDATA_rec, x = ~week , y = ~time_series_value,
                                 type = 'bar',
                                 marker = list(color = ~color), name = ~Year_indicator) %>%
      layout(title = "TEST",yaxis = list(title = 'Millions EUR '), barmode = 'stack')
    Chart<-ggplotly(Chart)

    Chart

因此下一个陡峭的地方是用绘图方式绘制此数据.这样您就可以看到我的情节如下所示:

So next steep is plot this data with plotly. So you can see how my plot look like below:

但是我的意图是像下面的图那样做图.我在Excel中作图,但我坚信我需要用plotly绘制图.最重要的是只比较相同的数据.例如2020年的数据包含大约16周的数据,评估必须与2019年同期进行.那么有人可以帮我解决这个问题,并用plotly绘制此图吗?

But my intention is to make plot like plot below.I plot in Excel but defently i need this plot with plotly.Most important thing is to compare only data which is same.For example data for 2020 contain data about 16 weeks and compratation must be with the same period of 2019. So can anybody help me about this problem and plot this plot with plotly ?

推荐答案

您需要为要绘制的每个时间序列添加trace,并在"plotly图"的layout中指定barmode.似乎不需要额外的数据操作即可获得所需的内容:

You need to add a trace for each time series you want to plot and specify barmode in the layout of your `plotly plot. No additional data manipulation seems necessary to get what you want:

代码

dat <- as.data.table(ARTIFICIALDATA)
colnames(dat) <- c('week', 'series1', 'series2')

plt <- plot_ly(dat) %>% 
  add_trace(x = ~week, y = ~series1, type = 'bar', name = '2019 Series 1') %>% 
  add_trace(x = ~week, y = ~series2, type = 'bar', name = '2020 Series 1') %>% 
  layout(
    xaxis = list(title = 'week'), 
    yaxis = list(title = ''),
    barmode = 'group'
  )

data.table部分不是必需的-我这样做仅仅是为了获得更简单的列名,因为我更喜欢使用data.table进行子集设置.

the data.table part is not necessary - I did that purely to get simpler column names and because I prefer data.table for subsetting etc.

输出

上面的代码返回以下图:

The above code returns the below plot:

您可以对数据进行子集化,以仅包括两个系列都有数据的星期,以便在您的帖子中显示图表.

You can subset your data to include only weeks for which both series have data to get the graph in your post.

plt <- plot_ly(dat[!is.na(series2)]) %>% 
   ...

(可选)您可以通过在layout中指定legend来将图例移至底部-依我之见,阅读起来会更好:

Optionally, you can move the legend to the bottom by specifying the legend in layout - makes it nicer to read in my opinion:

layout(
    ... 
    legend = list(orientation = 'h')
)

这给您:

这篇关于用PLOTLY绘制图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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