在x轴下创建包含每月数据的折线图

问题描述

我正尝试创建如下图表:

enter image description here

数据集如下:

import React from "react";
import ReactDOM from "react-dom";
import { Redirect } from "react-router-dom";

class Modal extends React.Component {
  state = {
    toDashboard: false,}

  handleClick = () => {
    this.setState(() => ({
      toDashboard: true
    }))
  }


  render() {
    if (this.state.toDashboard === true) {
      return <Redirect to='/' />
    }

    return ReactDOM.createPortal(
      
      <div
        className="ui dimmer modals visible active"
        onClick={ this.handleClick }
      >
        <div className="ui standard modal visible active">
          <div className="header">Delete Stream</div>
          <div className="content">
            Are you sure you want to delete this stream?
          </div>
          <div className="actions">
            <button className="ui primary button">Delete</button>
            <button className="ui button">Cancel</button>
          </div>
        </div>
      </div>,document.querySelector("#modal")
    );
  }
  
};

export default Modal;

我能够使用如下的plotly函数生成折线图:

+-----------+------+------------+------------+------------+
|           | Week | S19 retail | S20 target | S20 retail |
+-----------+------+------------+------------+------------+
| "November |      |            |            |            |
| +41.7%    |      |            |            |            |
| +56.5%"   |   45 |        134 |        216 |        152 |
|           |   46 |         89 |        129 |        142 |
|           |   47 |         93 |        132 |        133 |
|           |   48 |         94 |        166 |        154 |
| "December |      |            |            |            |
| +29%      |      |            |            |            |
| +26.3%"   |   49 |         97 |        166 |        182 |
|           |   50 |        108 |        166 |        161 |
|           |   51 |        108 |        153 |        132 |
|           |   52 |         99 |         83 |        121 |
|           |    1 |        140 |        129 |        116 |
| "January  |      |            |            |            |
| +2.4%     |      |            |            |            |
| -1.5%"    |    2 |        126 |         89 |        108 |
|           |    3 |        154 |        141 |        169 |
|           |    4 |        238 |        228 |        214 |
|           |    5 |        531 |        575 |        583 |
| "February |      |            |            |            |
| -20.9%    |      |            |            |            |
| +50.6%"   |    6 |        152 |        217 |        112 |
|           |    7 |        178 |        245 |        142 |
|           |    8 |        221 |        356 |        182 |
|           |    9 |        331 |        510 |        262 |
+-----------+------+------------+------------+------------+

我很难将每周数据下面的每月数据添加为所附图片中的数据

数据集

df<-read_excel("data.xlsx",sheet=4,skip=2,range = cell_cols("B:E"))

df<- reshape2::melt(df,id.vars = "Week")

plotly::plot_ly(df,x = ~Week,y = ~value,name = df$variable,type = 'scatter',mode = 'lines') 

解决方法

使用下一个代码,您可以接近问题的输出。下次,请按照 @BenNorris 的建议,使用dput()包含数据:

library(tidyverse)
#Code
df %>% fill(Var) %>% pivot_longer(-c(Var,Week)) %>%
  mutate(Var=gsub(' ','\n',trimws(Var))) %>%
  mutate(Var=factor(Var,levels = unique(Var),ordered = T)) %>%
  ggplot(aes(x=factor(Week),y=value,color=name,group=name))+
  geom_line()+
  geom_point()+
  facet_wrap(.~Var,scales = 'free_x',nrow = 1,strip.position = 'bottom')+
  theme_bw()+
  theme(strip.placement = 'outside',strip.background = element_blank(),legend.position = 'bottom')+xlab('')+
  labs(color='')

输出:

enter image description here

使用了一些数据:

#Data
df <- structure(list(Var = c("November 41.7% +56.5%",NA,"December 29% +26.3%","January 2.4% -1.5%","February -20.9% +50.6%",NA),Week = c(45,46,47,48,49,50,51,52,1,2,3,4,5,6,7,8,9),`S19 retail` = c(134,89,93,94,97,108,99,140,126,154,238,531,152,178,221,331),`S20 target` = c(216,129,132,166,153,83,141,228,575,217,245,356,510),`S20 retail` = c(152,142,133,182,161,121,116,169,214,583,112,262)),row.names = c(NA,-17L),class = "data.frame")

如果需要更多自定义,则可以使用以下方法:

#Code 2
df %>% fill(Var) %>% pivot_longer(-c(Var,legend.position = 'bottom',panel.border=element_blank())+xlab('')+
  labs(color='')

输出:

enter image description here

对于共享数据,您必须为这样的构面构建标签:

#Create label and plot
ndf %>% mutate(Lab=paste0(Month,100*(round(Growth.vs..LY,3)),'%',100*round(Growth.Target,3),'%')) %>%
  select(-c(Month,Growth.vs..LY,Growth.Target)) %>%
  pivot_longer(-c(Lab,Week)) %>%
  mutate(Lab=factor(Lab,levels = unique(Lab),group=name))+
  geom_line()+
  geom_point()+
  facet_wrap(.~Lab,panel.border=element_blank())+xlab('')+
  labs(color='')

输出:

enter image description here