绘制没有直线的时间序列

Plot time series without straight lines

我正在尝试绘制一些时间序列,但由于我只有夏季的数据,所以我得到了这些直线。知道如何解决这个问题吗?我使用的代码:任何想法都会有所帮助!

    ggplot(ba, aes(x=date1, y=pc1)) +
  geom_line(color="turquoise4") +
  theme_minimal() + 
  labs(x="", y="Loading", title="Correlation of PC1 and original series") +
  theme(plot.title = element_text(hjust=0.5, size=20, face="bold"))+
  scale_x_date(date_labels= ("%Y"))

剧情:

数据:structure(list(date1 = structure(c(10712, 10713, 10714, 10715, 10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724, 10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733, 10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742, 10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751, 10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760, 10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769, 10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778, 10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787, 10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796, 10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805, 10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814, 10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823, 10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832, 10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841, 10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850, 10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859, 10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081, 11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090, 11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099, 11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108, 11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117, 11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "日期"), pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087, 2.02249236956036、1.75829175459456、1.58770371446918、1.62230139615394、 1.73502227021784、1.75083678213192、1.64509065138032、1.57921033180313、 1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479, 1.9106874120324、1.73936455049801、1.50577059168685、1.24226003967481、 1.07813468676617、1.06276891964951、1.09622663209529、1.07692457712675、 0.978692818737612、1.06365064520783、1.2525349982313、1.08237838015766、 0.645239033194787, 0.479482241789711, 0.683701830568681, 0.792197472275541, 0.631531270886538, 0.5203​​37262457156, 0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418, 0.654712633988225, 0.440140874164089, 0.111631055132755, -0.22450806112272, -0.444238159039355, -0.584576558346287, -0.444097467542865, -0.227821057355029, -0.120935149111578, -0.0932195161137341, 0.037283855810637, 0.206479031035409, 0.173515424607062, 0.234536409515456, 0.317957256707112, 0.290090191780606, 0.0607339338833623, -0.27556992053308, -0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576, -0.79110428934397, -0.985635595643097, -0.994138228085185, -0.839909782593256, -0.699274458194957, -0.580683825031177, -0.530811870371419, -0.4746353951302, -0.489386570992314, -0.787222651887671, -1.1059671054324, -1.17983265148469, -1.1058432515423, -0.970485807735322, -0.679713450749357, -0.516950863200668, -0.495312393712548, -0.673645368786615, -0.792675131421433, -0.692021409445821, -0.611096320716252, -0.676712376641795, -0.723244566814595, -0.621986199057006, -0.563969216349158, -0.649311354664407, -0.679237194242732, -0.624476984795223, -0.738344795218295, -0.877867797047079, -0.879375052767018, -0.84262582765393, -0.845707036138972, -0.959691974084994, -1.06904324062176, -0.97905489332525, -0.847145240762566, -0.86837819324592, -0.935323976060101, -0.796486491787169, -0.461073031709012, -0.275818888900351, -0.513613296467615, -0.786611502858454, -0.799843667083875, -0.632676241199403, -0.468611824279096, -0.534017599627378, -0.501551518704511, -0.239313348556757, -0.208935210151003, -0.510483950549102, -0.62974750963569, -0.399113422985878, -0.072812659658845, 0.0377885597304766, -0.0102829082610216, -0.0571349366394233, -0.101917027852624, -0.202941574141862, -0.22849727264844, -0.125157862652187, 0.168703915373856, 0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287, -0.122289033919648, -0.288101855449495, -0.186400543130663, 0.0316721901308679, 0.121240481805255, -0.0753698973566349, -0.384779730900963, -0.531179497125517, -0.373632181420806, -0.0148926315001478, 0.146040939981569, 0.13371186468668, 0.200262938351445, 0.465073170745138, 0.506805629621484, 0.345398737766814, 0.171110245173291, 0.176555396235594, 0.262743070740985, 0.398601589660576, 0.433248104072272, 0.453883432665361, 0.604637145172226, 0.843278371818699, 1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484, 1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366, 2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636, 2.11677885248848、2.01466853738993、1.77967782944265、1.48938981000699、 1.34042958586002、1.33016846412245、1.31770813627339、1.26104969519401、 1.37385446004522、1.61517275597383、1.84510291043685、1.91280500843462、 1.84897419443657、1.52674793906846、1.29429812528379、1.06717755247561、 0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863, 1.04843964584885、1.10153891962662、1.15936307711726、1.20129772010444、 1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267, 0.655776789864271, 0.780033607405981, 0.664875593837605, 0.452000300833336, 0.394589410057676, 0.402170545544567, 0.403979206396259, 0.395485848597801, 0.433314713756909, 0.437960603442615)), row.names = c(NA, 200L), class = "data.frame")强文本

1) 使用末尾注释中的输入扩展日期以包括使用 NA 的缺失日期。然后绘图。

library(ggplot2)
library(zoo)

z <- read.zoo(ba)
zz <- merge(z, zoo(, seq(start(z), end(z), 1)))
autoplot(zz) + xlab("")

2) 另一种方法是对每年使用不同的方面。

library(ggplot2)

breaks <- unique(as.Date(cut(ba$date1, "month")))
ba2 <- transform(ba, year = as.integer(format(date1, "%Y")))
p <- ggplot(ba2, aes(date1, pc1)) + 
  geom_line() + 
  facet_grid(cols = vars(year), scales = "free_x", space = "free_x")
p + scale_x_date(breaks = breaks, date_labels = "%b")

(图后续)

或删除条形文本并仅将年份放在每个方面下方使用上面的 p 和

breaks <- as.Date(tapply(format(ba$date1), format(ba$date1, "%Y"), min))
p +
  scale_x_date(breaks = breaks, date_labels = "%Y") +
  theme(strip.text.x = element_blank())

3) facet 的想法也可以在 lattice 中实现。

library(lattice)

ba3 <- transform(ba, year = format(date1, "%Y"))
xyplot(pc1 ~ date1 | year, ba3, type = "l", 
  scales = list(x = list(relation = "free")), layout = c(NA, 1))

备注

问题中的 dput 输出有问题,因此使用了以下内容。

ba <-
structure(list(date1 = structure(c(10712, 10713, 10714, 10715, 
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724, 
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733, 
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742, 
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751, 
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760, 
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769, 
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778, 
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787, 
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796, 
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805, 
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814, 
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823, 
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832, 
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841, 
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850, 
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859, 
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081, 
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090, 
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099, 
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108, 
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117, 
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"), 
    pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087, 
    2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394, 
    1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313, 
    1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479, 
    1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481, 
    1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675, 
    0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766, 
    0.645239033194787, 0.479482241789711, 0.683701830568681, 
    0.792197472275541, 0.631531270886538, 0.520337262457156, 
    0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418, 
    0.654712633988225, 0.440140874164089, 0.111631055132755, 
    -0.22450806112272, -0.444238159039355, -0.584576558346287, 
    -0.444097467542865, -0.227821057355029, -0.120935149111578, 
    -0.0932195161137341, 0.037283855810637, 0.206479031035409, 
    0.173515424607062, 0.234536409515456, 0.317957256707112, 
    0.290090191780606, 0.0607339338833623, -0.27556992053308, 
    -0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576, 
    -0.79110428934397, -0.985635595643097, -0.994138228085185, 
    -0.839909782593256, -0.699274458194957, -0.580683825031177, 
    -0.530811870371419, -0.4746353951302, -0.489386570992314, 
    -0.787222651887671, -1.1059671054324, -1.17983265148469, 
    -1.1058432515423, -0.970485807735322, -0.679713450749357, 
    -0.516950863200668, -0.495312393712548, -0.673645368786615, 
    -0.792675131421433, -0.692021409445821, -0.611096320716252, 
    -0.676712376641795, -0.723244566814595, -0.621986199057006, 
    -0.563969216349158, -0.649311354664407, -0.679237194242732, 
    -0.624476984795223, -0.738344795218295, -0.877867797047079, 
    -0.879375052767018, -0.84262582765393, -0.845707036138972, 
    -0.959691974084994, -1.06904324062176, -0.97905489332525, 
    -0.847145240762566, -0.86837819324592, -0.935323976060101, 
    -0.796486491787169, -0.461073031709012, -0.275818888900351, 
    -0.513613296467615, -0.786611502858454, -0.799843667083875, 
    -0.632676241199403, -0.468611824279096, -0.534017599627378, 
    -0.501551518704511, -0.239313348556757, -0.208935210151003, 
    -0.510483950549102, -0.62974750963569, -0.399113422985878, 
    -0.072812659658845, 0.0377885597304766, -0.0102829082610216, 
    -0.0571349366394233, -0.101917027852624, -0.202941574141862, 
    -0.22849727264844, -0.125157862652187, 0.168703915373856, 
    0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287, 
    -0.122289033919648, -0.288101855449495, -0.186400543130663, 
    0.0316721901308679, 0.121240481805255, -0.0753698973566349, 
    -0.384779730900963, -0.531179497125517, -0.373632181420806, 
    -0.0148926315001478, 0.146040939981569, 0.13371186468668, 
    0.200262938351445, 0.465073170745138, 0.506805629621484, 
    0.345398737766814, 0.171110245173291, 0.176555396235594, 
    0.262743070740985, 0.398601589660576, 0.433248104072272, 
    0.453883432665361, 0.604637145172226, 0.843278371818699, 
    1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484, 
    1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366, 
    2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636, 
    2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699, 
    1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401, 
    1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462, 
    1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561, 
    0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863, 
    1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444, 
    1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267, 
    0.655776789864271, 0.780033607405981, 0.664875593837605, 
    0.452000300833336, 0.394589410057676, 0.402170545544567, 
    0.403979206396259, 0.395485848597801, 0.433314713756909, 
    0.437960603442615)), row.names = c(NA, -200L), class = "data.frame")