如何绘制R中10年数据的三个变量(如gdp,通货膨胀,失业率)

时间:2018-05-25 10:40:27

标签: r

我的数据:

    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

我想这样画:

Trend of unemployment rate, inflation rate and interest rate

我为gdp绘制了

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

但我需要gdp,通货膨胀,同样的失业率。

2 个答案:

答案 0 :(得分:0)

这是一个帮助您的简单示例

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()

enter image description here

答案 1 :(得分:0)

我这样做

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()