【问题标题】:Billboarder.R, grouped linechart with more than one variable to plotBillboarder.R,包含多个要绘制的变量的分组折线图
【发布时间】:2020-02-21 14:26:01
【问题描述】:

我正在尝试映射我的表。它绘制在具有一年中的第几周 (1-53) 的 x 轴和具有特定百分比 (0-100) 的 y 轴上。在这个图中,我尝试制作两条线,一条用于变量“Task”,另一条用于变量“Area”。但是,由于 x 轴只到一年,所以我还希望每年都有一条新线。

我的数据如下:

head(dt.Ratio()[Week %in% c(52, 53, 1, 2, 3)])
       year Week  Area  Task
    1: 2019   52 63.68 28.39
    2: 2019   53  3.23  0.00
    3: 2020    1 58.58 25.43
    4: 2020    2 61.54 31.75
    5: 2020    3 52.33 27.10

情节是这样完成的:

billboarder() %>%
        bb_linechart(dt.Ratio(), show_point = TRUE, type = "area") %>%
        bb_x_axis(label = list(text = "Week", position = "outer-right"),
                  tick = list(culling = list(max = 1))) %>%
        bb_y_axis(label = list(text = "Ratio of hours clocked as task", position = "outer-right")) %>%
        bb_y_grid(show = TRUE) %>%
        bb_colors_manual(opacity = 0.25)

我尝试了很多方法来处理 bb_linechart 中的映射变量,但我找不到正确的映射。我可以使其适用于 Area 或 Task 或不按年份分组,但我还没有找到包含所有 4 行(2019 年和 2020 年,变量 Task 和 Area)的解决方案

【问题讨论】:

    标签: r plot billboard.js


    【解决方案1】:

    您好,您需要对数据进行一些转换。

    第一个选项:使用日期

    library(data.table)
    library(billboarder)
    
    dt <- fread(text = "year Week  Area  Task
    2019   52 63.68 28.39
    2020    1 58.58 25.43
    2020    2 61.54 31.75
    2020    3 52.33 27.10")
    
    # Convert date
    dt[, date := as.Date(paste(year, Week, 1, sep = "-"), format = "%Y-%U-%u")]
    
    # Pivot data
    dt <- melt(
      data = dt, 
      id.vars = "date",
      measure.vars = c("Area", "Task")
    )
    
    # Plot
    billboarder() %>%
      bb_linechart(dt, bbaes(x = date, y = value, group = variable), show_point = TRUE, type = "area") %>%
      bb_x_axis(label = list(text = "Week", position = "outer-right"),
                tick = list(culling = list(max = 1))) %>%
      bb_y_axis(label = list(text = "Ratio of hours clocked as task", position = "outer-right")) %>%
      bb_y_grid(show = TRUE) %>%
      bb_colors_manual(opacity = 0.25)
    

    第二个选项:将年份和星期连接为字符串

    library(data.table)
    library(billboarder)
    
    dt <- fread(text = "year Week  Area  Task
    2019   52 63.68 28.39
    2020    1 58.58 25.43
    2020    2 61.54 31.75
    2020    3 52.33 27.10")
    
    # Or just concatenate year and week
    dt[, year_week := paste(year, Week, sep = "-")]
    
    # Pivot data
    dt <- melt(
      data = dt, 
      id.vars = "year_week",
      measure.vars = c("Area", "Task")
    )
    
    # Plot
    billboarder() %>%
      bb_linechart(dt, bbaes(x = year_week, y = value, group = variable), show_point = TRUE, type = "area") %>%
      bb_x_axis(
        type = "category",
        label = list(text = "Week", position = "outer-right"),
        tick = list(culling = list(max = 1))
      ) %>%
      bb_y_axis(label = list(text = "Ratio of hours clocked as task", position = "outer-right")) %>%
      bb_y_grid(show = TRUE) %>%
      bb_colors_manual(opacity = 0.25)
    

    你必须在bb_x_axis中使用type = "category",结果有点不同:

    【讨论】:

    • 感谢您的回复。一旦您拥有超过一年的数据,这些方法不会导致 x 轴长于 53 吗?我想看看 2019 年第 1 周与 2020 年第 1 周在 x 轴上的同一位置。这样我可以轻松比较增长。还是你不推荐?
    • 实际上更多地研究了 r 脚本并重写了它以匹配您的第二个解决方案,我真的很喜欢这个结果!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2021-11-21
    • 1970-01-01
    • 2023-01-08
    • 2019-10-31
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多