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

问题描述 投票:-2回答:1

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

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

我认为这是一个离散变量和连续变量问题,但我不确定。愿有人指出我正确的方向吗?我是否必须将“年份”列(当前是字符向量)更改为日期数据类型?

r ggplot2 dplyr
1个回答
1
投票

从美学中删除引号。

© www.soinside.com 2019 - 2024. All rights reserved.