Create specific date range in ggplot2 (scale_x_date)

Hi I have data for a year from 2010 to 2050. I'm trying to make a line graph so that my x-axis starts from 2010 and ends in 2050 showing a 5 year interval and I want the x-axis break (2010, 2015, 2020, ...., 2045, 2050 ). Unfortunately, this starts in 2013 and ends in 2048. Why? How to fix it? 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))

      

enter image description here

+3


source to share


1 answer


If you know exactly where your break values ​​should be, just say 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))

      



enter image description here

+11


source







All Articles