jiedian <- read.csv("E:\\e-program\\在用数据\\edata综合版-去掉金融业(least).csv")
eco <- read.csv("E:\\e-program\\在用数据\\经济数据-2.csv")
jiedian$年月 <-  seq(as.Date("2016-01-01"),length = 83,by = "1 month" )
eco$年月 <-  seq(as.Date("2016-01-01"),length = 83,by = "1 month" )
library(dplyr)
library(ggplot2)
library(forecast)
library(pheatmap)
library(tidyverse)
library(lubridate)
data_classify <- function(data,i){
  a <- data %>%
    filter(`index1`==i) %>%
    select(`年月`,`业扩类型`,`接电容量`,`申请容量`,`一级行业`)
  return(a)
}
Eco_classify <- function(data,i){
  a <- data %>%
    filter(`index2`==i)%>%
    select(`年月`,`经济数值`,`经济指标`)
  return(a)
}
moving_cor <- function(start_date,data,window_length){
  data %>% 
    filter(between(`年月`,start_date-months(window_length),start_date+months(window_length))) %>%
    select(`接电容量`,`申请容量`,`业扩类型`,`经济数值`) %>%
    group_by(`业扩类型`) %>%
    summarise(
      `年月` = start_date,
      `业扩类型` = first(`业扩类型`),
      `接电相关系数`=cor(`接电容量`,`经济数值`),
      `申请相关系数`=cor(`申请容量`,`经济数值`))
}
mean_pearson_year <- function(data,year,window_length){
  start_date <- seq(as.Date("2016-06-01"),length = 78,by = "1 month" )
  b <- map_dfr(start_date,~(moving_cor(.x,data,window_length)))
  b$年月 <- year(b$年月)
  b %>% 
    filter(`年月`==year) %>%
    group_by(`业扩类型`)%>%
    summarise(
    `年份`=year,
    `业扩类型`=first(`业扩类型`),
    `接电相关系数均值`=mean(`接电相关系数`),
    `申请相关系数均值`=mean(`申请相关系数`)
   )
}
# ?layout
#for(j in c(1:7)){
pheatmap_df <- function(year){
  df <- data.frame()
  for(i in c(0:10)){
    df[c(1:6),i+1]<-mean_pearson_year(merge(data_classify(jiedian,i),q,by="年月",all=T),k,5)$接电相关系数均值
    df[c(7:12),i+1]<-mean_pearson_year(merge(data_classify(jiedian,i),q,by="年月",all=T),k,5)$申请相关系数均值
  }
  colnames(df)<-c("the whole society","agriculture .etc","industry",
                  "construction","transportation.etc","information.etc",
                  "wholesale&retail'","accommodation&catering",
                  "estate","leasing&business",
                  "Public Service.etc")
  rownames(df)<-c("New-installation(jie)","add-capacity(jie)","reduction(jie)","Close-account(jie)","pause(jie)","recovery(jie)",
                  "New-installation","add-capacity","reduction","Close-account","pause","recovery")
  return(df)
}
for (j in c(1:7)) {
  q <- Eco_classify(eco,j)
  print(2016,"~",2022,"-",q$经济指标[1])
  for (k in c(2016:2022)) {
    bk <- c(seq(-0.8,-0.1,by=0.3),seq(0,0.8,by=0.2))
pheatmap(pheatmap_df(k),color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/2),colorRampPalette(colors = c("white","red"))(length(bk)/2)),
         legend_breaks=seq(-0.8,0.8,0.2),cluster_row =FALSE , cluster_cols = FALSE,
         breaks=bk,display_numbers = T)
  }
}

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

{r cars} summary(cars)

Including Plots

You can also embed plots, for example:

{r pressure, echo=FALSE} plot(pressure)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.