代码之家  ›  专栏  ›  技术社区  ›  loki

如何使用r corrplot函数绘制Excel中的数据?

  •  1
  • loki  · 技术社区  · 7 年前

    我正在尝试学习r,并使用corrpot库绘制 y:city x:population graph。我写了下面的代码:

    当你看上面的图片时,有两列城市和人口。当我运行代码时,会收到以下错误消息:

    error in cor(illere-gore-nufus):'x'must be numeric.

    我的Excel数据:

    .

    当你看上面的图片时,有两列城市和人口。当我运行代码时,会收到以下错误消息:

    Error in cor(Illere_Gore_Nufus) : 'x' must be numeric.

    我的Excel数据:

    2 回复  |  直到 7 年前
        1
  •  1
  •   vestland    7 年前

    Country         2017        2016        2015        2014        2013
    China           1415045928  1412626453  1414944844  1411445597  1409517397
    India           1354051854  1340371473  1339431384  1343418009  1339180127
    United States   326766748   324472802   325279622   324521777   324459463
    Indonesia       266794980   266244787   266591965   265394107   263991379
    Brazil          210867954   210335253   209297939   209860881   209288278
    Pakistan        200813818   199761249   200253292   197655630   197015955
    Nigeria         195875237   192568158   195757661   191728478   190886311
    Bangladesh      166368149   165630262   165936711   166124290   164669751
    Russia          143964709   143658415   143146914   143341653   142989754
    Mexcio          137590740   137486490   136768870   137177870   136590740
    

    cor()

    library(corrplot)
    library(readxl)
    
    # Read data
    setwd("C:/")
    data <- read_excel("data.xlsx")
    
    # Set Country names as row index
    rownames(data) <- data$Country
    
    # Remove Country from dataframe
    data$Country <- NULL
    
    # Transpose data into a readable format for cor()
    data <- data.frame(t(data))
    
    # Plot data
    corrplot(cor(data))
    

    CorrPlot

    Country,2017,2016,2015,2014,2013
    China,1415045928,1412626453,1414944844,1411445597,1409517397
    India,1354051854,1340371473,1339431384,1343418009,1339180127
    United States,326766748,324472802,325279622,324521777,324459463
    Indonesia,266794980,266244787,266591965,265394107,263991379
    Brazil,210867954,210335253,209297939,209860881,209288278
    Pakistan,200813818,199761249,200253292,197655630,197015955
    Nigeria,195875237,192568158,195757661,191728478,190886311
    Bangladesh,166368149,165630262,165936711,166124290,164669751
    Russia,143964709,143658415,143146914,143341653,142989754
    Mexcio,137590740,137486490,136768870,137177870,136590740
    

    countries.csv

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    library(corrplot)
    
    # Set Country names as row index
    data <- dataset
    
    rownames(data) <- data$Country
    
    # Remove Country from dataframe
    data$Country <- NULL
    
    # Transpose data into a readable format for cor()
    data <- data.frame(t(data))
    
    # Plot data
    corrplot(cor(data))
    

    enter image description here

    enter image description here

    enter image description here

        2
  •  1
  •   Manoraj    7 年前

    Here is the Data

    Scatter plot

    area<-c(650,785,880,990,1100,1250,1350,1800,2200,2800)
    price<-c(250,275,280,290,350,340,400,335,420,460)
    cor(area,price)
    plot(area,price)
    

    Scatter plot