【问题标题】:how to plot three varaibles(like gdp,inflation,unemployment) for 10 years data in R如何在 R 中绘制 10 年数据的三个变量(如 gdp、通货膨胀、失业)
【发布时间】:2018-11-04 17:50:11
【问题描述】:

我的数据:

    Country       Year  FY_sales Truck_type       GDP     Inflation_Rate Unemployment_Rate

 1  France 2007-05-25  2064543        LCV 2663112510266    1.488073528       7.659999847
 2  France 2007-05-25   460552     MCV/CV 2663112510266    1.488073528       7.659999847
 3  France 2007-05-25    58940        HCV 2663112510266    1.488073528       7.659999847

我想像这样绘制:

我已经为 gdp 绘制了:

ggplot(data,aes(Year,gdp))+geom_line()+geom_point()

但我需要 gdp、通货膨胀、失业。

【问题讨论】:

标签: r


【解决方案1】:

我做这样的事情

library(data.table)

setDT(dt)

dataGraf <- rbind(data[ ,.(Year, value = Unemployment_Rate, Type = "Unemployment_Rate")],
                  data[ ,.(Year, value = Inflation_Rate, Type = "Inflation_Rate")],
                  data[ ,.(Year, value = GDP, Type = "GDP")])

ggplot(dataGraf,aes(Year, value, color = Type))+geom_line()+geom_point()

【讨论】:

    【解决方案2】:

    这是一个帮助你的简单例子

    set.seed(5)
    
    # example data
    dt = data.frame(id = 1:4,
                    x = runif(4),
                    y = runif(4),
                    z = runif(4))
    
    library(tidyverse)
    
    dt %>%
      gather(var, value, -id) %>%        # reshape data
      ggplot(aes(id, value, col=var))+   # plot using different colour for each variable
      geom_point()+
      geom_line()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 2017-03-14
      • 2018-06-26
      • 2012-04-11
      • 2023-02-24
      相关资源
      最近更新 更多