折线图问题 - 情节看起来 "funny" (ggplot2)

Line chart issues - plot looks "funny" (ggplot2)

我有一个大型数据框 (CO2_df),其中包含许多国家多年的数据,并尝试用 ggplot2 绘制图表。该图将有 6 条曲线 + 一条聚合曲线。但是,我的图表看起来很漂亮 "funny",我不知道为什么。

数据如下所示(摘录):

       x     y          x1      x2      x4   x6
1553   1993  0.00000    CO2     Austria  6   6 - Other Sector
1554   2006  0.00000    CO2     Austria  6   6 - Other Sector
1555   2015  0.00000    CO2     Austria  6   6 - Other Sector
2243   1998  12.07760   CO2     Austria  5   5 - Waste management
2400   1992  11.12720   CO2     Austria  5   5 - Waste management
2401   1995  11.11040   CO2     Austria  5   5 - Waste management
2402   2006  10.26000   CO2     Austria  5   5 - Waste management
2489   1998  0.00000    CO2     Austria  6   6 - Other Sector

我用过这个代码:

ggplot(data=CO2_df, aes(x=x, y=y, group=x6, colour=x6)) +
  geom_line() +
  geom_point() +
  ggtitle("Austria") +
  xlab("Year") +
  ylab("C02 Emissions") +
  labs(colour = "Sectors")
scale_color_brewer(palette="Dark2")  

CO2_df %>%
  group_by(x) %>%
  mutate(sum.y = sum(y)) %>%
  ggplot(aes(x=x, y=y, group=x6, colour=x6)) +
  geom_line() +
  geom_point() +
  ggtitle("Austria") +
  xlab("Year") +
  ylab("C02 Emissions") +
  labs(colour = "Sectors")+
  scale_color_brewer(palette="Dark2")+
  geom_line(aes(y = sum.y), color = "black") 

我的问题

1) 为什么会这样,我该如何解决? 2) 我不知道为什么 y 轴上的值接近于零。他们不是... 3) 如何在聚合线的图例中添加条目?

感谢您的帮助!

北海

像这样的事情怎么样:

  CO2_df %>%                            # data                 
  group_by(x,x6) %>%                    # group by
  summarise(y = sum(y)) %>%             # add the sum per group
  ggplot(aes(x=x, y=y)) +               # plot
  geom_line(aes(group=x6, fill=x6, color=x6))+
  # here you can put a summary line, like sum, or mean, and so on
  stat_summary(fun.y = sum, na.rm = TRUE, color = 'black', geom ='line') +
  geom_point() +
  ggtitle("Austria") +
  xlab("Year") +
  ylab("C02 Emissions") +
  labs(colour = "Sectors")+
  scale_color_brewer(palette="Dark2"))

使用修改后的数据,为了看到正确的行为,我使用了相同的年份和非常不同的值来理解:

   CO2_df <- read.table(text ="
x     y          x1      x2      x4   x6
1553   1993  20    CO2     'Austria'  6   '6 - Other Sector'
1554   1994  23    CO2     'Austria'  6   '6 - Other Sector'
1555   1995  43    CO2     'Austria'  6   '6 - Other Sector'
2243   1993  12.07760   CO2     'Austria'  5   '5 - Waste management'
2400   1994  11.12720   CO2     'Austria'  5   '5 - Waste management'
2401   1995  11.11040   CO2     'Austria'  5   '5 - Waste management'
2402   1996  10.26000   CO2     'Austria'  5   '5 - Waste management'
2489   1996  50    CO2     'Austria'  6   '6 - Other Sector'", header = T)