繁体   English   中英

绘制时间序列图 R 中的 Ggplot2 错误

[英]Plotting a time series graph Error with Ggplot2 in R

我有以下数据集

# A tibble: 11 x 3
  Anio  Porc_PBI
 <dbl> <chr>       <dbl>
 1  2008 BPS       6.42
 2  2009 BPS       6.52
 3  2010 BPS       6.98
 4  2011 BPS       6.93
 5  2012 BPS       7.88
 6  2013 BPS       7.93
 7  2014 BPS       7.97
 8  2015 BPS       8.26
 9  2016 BPS       8.43
 10 2017 BPS       8.83
 11 2018 BPS       9.05

我想 plot 显示 Porc_PBI 变量的时间演变的时间序列图。 因此,我想要一个带有 Anio 变量的 x 轴和一个带有 Porc_PBI 变量的 y 轴变量。 我尝试使用以下代码:

ggplot(dataset, aes(x = Anio, y=Porc_PBI)) +
 geom_line(color="red", size=2, linetype="dotted")+
 geom_point(color="blue", size=3)  +
 xlab("Año") +
 ylab("Porcentaje del PBI (%)")+
 scale_x_discrete(breaks=seq(2008,2018, by=1),
               labels=seq(2008,2018, by=1)
 )

结果如下:

时间序列图

如何修复 x 轴标签并在其上显示 2008 年至 2018 年的所有年份?

谢谢

使用scale_x_continuous而不是scale_x_discrete

ggplot(dataset, aes(x = Anio, y = Porc_PBI)) +
  geom_line(color="red", size=2, linetype="dotted")+
  geom_point(color="blue", size=3)  +
  xlab("Año") +
  ylab("Porcentaje del PBI (%)") +
  scale_x_continuous(breaks = seq(2008, 2018, by = 1))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM