在ggplot2中创建特定的日期范围(scale_x_date) [英] create specific date range in ggplot2 ( scale_x_date)

查看:1642
本文介绍了在ggplot2中创建特定的日期范围(scale_x_date)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有从2010年到2050年的年度数据。
i正在尝试做线图,以便我的x轴从2010年开始,并在2050年结束,显示5年的时间间隔iei想要x轴突破为(2010,2015 ,2020,...,2045,2050)。不幸的是,它从2013年开始,到2048年结束。为什么?如何解决这个问题? b

Hi i have yearly data from 2010 to 2050. i am trying make line plot so that my x-axis start from 2010 and end in 2050 showing 5 years interval i e i want x-axis break as ( 2010, 2015, 2020,....,2045,2050). Unfortunately it starts from 2013 and end at 2048. why ? how to fix this ? my data, code and result image are below.

plot1<- ggplot(test, aes(yr, y=value)) +
geom_line()+
scale_x_date(breaks = date_breaks("5 year"),labels=date_format("%Y")) +
scale_y_continuous(breaks=seq(-4,6, by=1))
plot1

DATA

 dput(test)
structure(list(value = c(2.47099989218436, 3.09640452148661, 
1.32121989082519, 0.742309399667898, 0.180070229458727, 2.2475619117108, 
0.606470664265897, 2.12742331755353, 2.73894680327422, 2.22368873465667, 
1.58381022102847, 2.10136510397371, 1.74582199030396, 2.21689521610787, 
2.51618709804907, 1.87243814589322, 1.92029935267449, 1.79383249007138, 
0.537680017904451, 1.2415782984683, 3.62075008273724, 4.50975793125965, 
3.70660640492563, 4.16317150909305, 2.24008439109747, 2.24587596633027, 
3.63019754286973, 4.28513572439197, 3.61013179034863, 4.20010027834161, 
2.06766292535187, 4.34833637648799, 5.71460894423653, 4.12185659615561, 
3.93305702163007, 3.29384139246081, 3.2915580598453, 4.21009646693621, 
4.32889796119913, 4.99213117815761), yr = structure(c(14610, 
14975, 15340, 15706, 16071, 16436, 16801, 17167, 17532, 17897, 
18262, 18628, 18993, 19358, 19723, 20089, 20454, 20819, 21184, 
21550, 21915, 22280, 22645, 23011, 23376, 23741, 24106, 24472, 
24837, 25202, 25567, 25933, 26298, 26663, 27028, 27394, 27759, 
28124, 28489, 28855), class = "Date")), .Names = c("value", "yr"
), class = "data.frame", row.names = c(NA, 40L))

推荐答案

如果你确切地知道你的中断值的位置,那就告诉 ggplot

If you know exactly where you want your break values to be, then just tell ggplot

plot1<- ggplot(test, aes(yr, y=value)) +
geom_line()+
scale_x_date(breaks = seq(as.Date("2010-01-01"), as.Date("2050-12-31"), by="5 years"), 
    labels=date_format("%Y")) +
scale_y_continuous(breaks=seq(-4,6, by=1))

这篇关于在ggplot2中创建特定的日期范围(scale_x_date)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆