簡體   English   中英

繪制長時間序列?

[英]Plotting through a long time series?

我有一個提供有關產品信息的數據集,我想在2年內每月繪制一些統計數據。 我已經完成了達到我想要的階段所需的一切。

df <- structure(list(how_many = c(14L, 654L, 8L, 373L, 33L, 240L, 48L, 
242L, 2L, 45L, 239L, 5L, 29L, 206L, 20L, 29L, 194L, 49L, 25L, 
143L, 17L, 21L, 121L, 12L, 22L, 83L, 1L, 20L, 90L, 15L, 713L), 
    prod_vers = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 1L, 2L), .Label = c("v1", "v2", "v3"), class = "factor"), 
    when = structure(c(16801, 16801, 16832, 16832, 16495, 16861, 
    16526, 16892, 16892, 16556, 16922, 16922, 16587, 16953, 16953, 
    16617, 16617, 16983, 16648, 17014, 17014, 16679, 16679, 17045, 
    16709, 17075, 17075, 16740, 16740, 16770, 16770), class = "Date")), .Names = c("how_many", 
"prod_vers", "when"), row.names = c(NA, -31L), class = "data.frame")

    how_many  prod_vers   when
1        14          v1 2016-01
2       654          v2 2016-01
3         8          v1 2016-02
4       373          v2 2016-02
5        33          v1 2015-03
6       240          v2 2016-03
7        48          v1 2015-04
8       242          v2 2016-04
9         2          v3 2016-04
10       45          v1 2015-05
11      239          v2 2016-05
12        5          v3 2016-05
13       29          v1 2015-06
14      206          v2 2016-06
15       20          v3 2016-06
16       29          v1 2015-07
17      194          v2 2015-07
18       49          v3 2016-07
19       25          v1 2015-08
20      143          v2 2016-08
21       17          v3 2016-08
22       21          v1 2015-09
23      121          v2 2015-09
24       12          v3 2016-09
25       22          v1 2015-10
26       83          v2 2016-10
27        1          v3 2016-10

但是我不知道如何繪制它。 我嘗試了barplot,但是沒有用。 我也考慮過要這樣繪制: 考慮大量月份及其相應的產品版本

一個包含2015年及其月份和版本的地塊,另一個包含2016年的地塊。 我不確定它是否看起來整潔

同樣,每個月在各自的版本中繪制每個版本的圖也會很多。

總結一下我該如何繪制?

數據集: mediafire.com/file/tt2l50nz4dzgaw6/DataforSover.csv

df <- structure(list(how_many = c(14L, 654L, 8L, 373L, 33L, 240L, 48L, 
242L, 2L, 45L, 239L, 5L, 29L, 206L, 20L, 29L, 194L, 49L, 25L, 
143L, 17L, 21L, 121L, 12L, 22L, 83L, 1L, 20L, 90L, 15L, 713L), 
    prod_vers = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
    1L, 2L, 3L, 1L, 2L, 1L, 2L), .Label = c("v1", "v2", "v3"), class = "factor"), 
    when = structure(c(16801, 16801, 16832, 16832, 16495, 16861, 
    16526, 16892, 16892, 16556, 16922, 16922, 16587, 16953, 16953, 
    16617, 16617, 16983, 16648, 17014, 17014, 16679, 16679, 17045, 
    16709, 17075, 17075, 16740, 16740, 16770, 16770), class = "Date")), .Names = c("how_many", 
"prod_vers", "when"), row.names = c(NA, -31L), class = "data.frame")

df$when <- as.Date(paste0(as.character(df$when), "-01"))

library(ggplot2)
ggplot(df, aes(x = when, y = how_many)) +
  geom_line() + 
  facet_grid(~prod_vers)

在此處輸入圖片說明

這個怎么樣? 您可以在facet_grid使用scales選項(例如scales = 'free_y' )以對構面使用不同的比例。

您可以使用構面按組將數據分開,如下所示:

data <- read.csv("DataforSover.csv")

colnames(data) <- c("X", "count", "app_version", "dt")

data$dt <- as.character(data$dt)

ggplot(data, aes(x = dt, y = count)) +
  geom_bar(stat="identity") +
  facet_grid(app_version ~ .) +
  xlab("Month") +
  ylab("Count") +
  ggtitle("Count by Month & App Version")

ggplot(data, aes(x = dt, y = count)) +
  geom_bar(stat="identity") +
  facet_grid(app_version ~ substring(dt, 1, 4)) +
  xlab("Month") +
  ylab("Count") +
  ggtitle("Count by Month, Year & App Version")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM