试图用R表示每年用电量的变化

时间:2018-03-05 17:34:52

标签: r ggplot2 dplyr

编辑:我发现年份必须是数字数据类型,或者代码必须写成:

ggplot(data = Electricity_Consumption_per_Capita_United_States, aes(x = year, y = value)) +
  geom_line(group = 1) +
  scale_x_continuous(breaks = seq(1960, 2010, 5))

原始问题:

我下载了"每人使用电费"来自here的数据集。这就是数据框的样子:

enter image description here

我试图在数据框架(1960年至2011年)中绘制多年来任何特定国家的人均用电量变化,并决定从美国开始。我认为使用tidyr组织一列下的年份和另一列下的实际kWh是有意义的:

Electricity_Consumption_Per_Capita <- read_excel("Datasets/Indicator_Electricity consumption per capita.xlsx")

#Gather the years and corresponding electricity consumption per capita values per country.
Electricity_Consumption_Per_Capita %>%
  gather(key = "year", value = "value", -"Electricity consumption, per capita (kWh)") -> Electricity_Consumption_Per_Capita

#Rename the Electricity consumption, per capita (kWh) variable to Country, then filter to obtain the data for the United States.
Electricity_Consumption_Per_Capita %>%
  rename(Country = `Electricity consumption, per capita (kWh)`) %>%
  group_by(Country) %>%
  filter(Country == "United States") -> Electricity_Consumption_per_Capita_United_States

结果数据框如下所示:

enter image description here

不幸的是,我无法弄清楚如何在同一地块上绘制价值(千瓦时)和年份。我尝试了一个正常的折线图但没有成功:

ggplot(data = Electricity_Consumption_per_Capita_United_States, aes(x = "year", y = "value")) +
  geom_line()

我认为这是一个离散变量和连续变量问题,但我不确定。愿有人指出我正确的方向吗?我是否需要改变&#34;年份&#34;列,当前是字符向量,到日期数据类型?

1 个答案:

答案 0 :(得分:1)

从美学中删除引号。