覆盖x轴刻度

时间:2016-01-28 21:11:40

标签: r ggplot2

我通过回归中的某些点运行spline,我想用ggplot2绘制它们,其中x刻度是年,但我不知道如何做到这一点。

如何覆盖从1920年到1950年的x-scale?

数据:

df <- structure(list(x = 1:200, y = c(0.00122973667762024, 6.62098801946071e-05, 
-0.000959979058174531, -0.00185343528846307, -0.00261876396164689, 
-0.0032605702287019, -0.00378345924060399, -0.00419203614832906, 
-0.00449090610285299, -0.00468467425515169, -0.00477794575620104, 
-0.00477532575697695, -0.0046814194084553, -0.00450083186161199, 
-0.00423816826742291, -0.00389803377686397, -0.00348503354091104, 
-0.00300377271054004, -0.00245885643672684, -0.00185488987044735, 
-0.00119647878632586, -0.000489056540407894, 0.000259484465636334, 
0.00104079719045607, 0.00184653459270055, 0.00266834963101903, 
0.00349789526406075, 0.00432682445047494, 0.00514679014891087, 
0.00594944531801776, 0.00672644291644486, 0.00746943590284142, 
0.00817007723585667, 0.00882001987413988, 0.00941091677634026, 
0.00993442090110708, 0.0103821852070896, 0.010745862652937, 0.0110171061972986, 
0.0111875687988235, 0.011248928362097, 0.0111981513301005, 0.0110440035734643, 
0.0107968475027119, 0.010467045528367, 0.010064960060953, 0.00960095351099359, 
0.00908538828901222, 0.00852862680553249, 0.00794103147107794, 
0.00733296469617213, 0.00671478889133861, 0.00609686646710094, 
0.00548955983398266, 0.00490323140250733, 0.00434824358319851, 
0.00383495878657975, 0.00337373942317461, 0.00297494790350662, 
0.00264894663809936, 0.00240601257949406, 0.00224981076448868, 
0.00217279857375538, 0.00216634775507979, 0.00222183005624753, 
0.00233061722504423, 0.00248408100925552, 0.00267359315666704, 
0.00289052541506439, 0.00312624953223322, 0.00337213725595915, 
0.00361956033402782, 0.00385989051422484, 0.00408449954433585, 
0.00428475917214646, 0.00445204114544233, 0.00457771721200906, 
0.00465315911963229, 0.00466973861609765, 0.00461882744919076, 
0.00449196986691963, 0.0042874160634374, 0.00401212749412751, 
0.00367364780262395, 0.0032795206325607, 0.00283728962757174, 
0.00235449843129108, 0.00183869068735268, 0.00129741003939055, 
0.000738200131038661, 0.000168604605931003, -0.000403832892298435, 
-0.000971568720015669, -0.00152705923358671, -0.00206276078937758, 
-0.00257112974375428, -0.00304462245308283, -0.00347569527372924, 
-0.00385680456205953, -0.00418040667443971, -0.00443922939659004, 
-0.00463224338937841, -0.00476466218882034, -0.00484197076028562, 
-0.00486965406914401, -0.00485319708076528, -0.00479808476051921, 
-0.00470980207377557, -0.00459383398590413, -0.00445566546227465, 
-0.00430078146825693, -0.00413466696922072, -0.00396280693053579, 
-0.00379068631757193, -0.00362379009569889, -0.00346760323028646, 
-0.00332761068670441, -0.0032092974303225, -0.00311814842651051, 
-0.00305964864063822, -0.00303897089201381, -0.00305661736998701, 
-0.00310949480371679, -0.00319441743464015, -0.00330819950419407, 
-0.00344765525381556, -0.00360959892494162, -0.00379084475900925, 
-0.00398820699745545, -0.00419849988171722, -0.00441853765323156, 
-0.00464513455343546, -0.00487510482376593, -0.00510526270565997, 
-0.00533242244055458, -0.00555339826988675, -0.00576500443509349, 
-0.00596405517761179, -0.00614736473887866, -0.00631174736033109, 
-0.00645423379806727, -0.00657409002222406, -0.00667190065928587, 
-0.00674826737916529, -0.00680379185177487, -0.00683907574702718, 
-0.0068547207348348, -0.0068513284851103, -0.00682950066776623, 
-0.00678983895271517, -0.00673294500986969, -0.00665942050914235, 
-0.00656986712044573, -0.00646488651369238, -0.00634508035879489, 
-0.00621105032566582, -0.00606339808421773, -0.0059027253043632, 
-0.00572963365601479, -0.00554472480908507, -0.0053486844807736, 
-0.00514281955026036, -0.00492871530336347, -0.00470795833913992, 
-0.0044821352566467, -0.0042528326549408, -0.0040216371330792, 
-0.0037901352901189, -0.00355991372511687, -0.00333255903713012, 
-0.00310965782521562, -0.00289279668843038, -0.00268356222583137, 
-0.00248354103647558, -0.00229431971942, -0.00211748487372163, 
-0.00195462309843743, -0.00180732099262442, -0.00167716515533957, 
-0.00156574218563988, -0.00147461474880411, -0.00140521605759051, 
-0.00135893575805815, -0.00133716346343512, -0.00134128878694955, 
-0.00137270134182952, -0.00143279074130315, -0.00152294659859854, 
-0.0016445585269438, -0.00179901613956701, -0.00198770904969631, 
-0.00221202687055977, -0.00247335921538553, -0.00277309569740165, 
-0.00311262592983628, -0.00349333952591749, -0.00391662609887341, 
-0.00438387526193212, -0.00489647662832175, -0.00545581981127037
)), .Names = c("x", "y"), row.names = c(NA, -200L), class = "data.frame")

我试过这个,但没有运气:

ggplot(df, aes(x = x, y = y)) + geom_line() + scale_x_discrete(breaks = 1:31, labels = seq(1920,1950))

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以将tidyr::seq_range()用于此

library(ggplot2)
library(tidyr)
df$x <- seq_range(1920:1950,dim(df)[1])
ggplot(df, aes(x = x, y = y) ) + geom_line()

enter image description here