在ggplot2中按年份分组刻度标记

时间:2016-06-19 01:22:16

标签: r ggplot2

我有一个时间序列,在生长季节的早期和晚期测量茎密度。我想介绍一下干密度随时间变化的趋势,但包括2005年早期,2005年晚期,2006年早期,2006年晚期等刻度标记标签太过杂乱无法展示。是否可以对每年的刻度线进行分组,这样每个标记只能表示"早期"或"晚期",每一对下方都显示年份?

这是我到目前为止所提出的,但我事先知道它不会完全给我我的想法,但更糟糕的是,它不起作用。它会产生一个没有任何x轴刻度标签的图:

dt<-ggplot(nzsum, aes(x = Date, y = Average.Stem.Density, colour = Species)) + geom_line(aes(group = Species)) + 
geom_point() + geom_errorbar(aes(ymin=Average.Stem.Density-se, ymax=Average.Stem.Density+se), width = 0.25) + facet_grid(Heat~Treatment)
dt<-dt+ scale_x_discrete(breaks = 1:18, labels = c("2005 Early", "Late", "2006 Early", "Late", "2007 Early","Late","2008 Early", "Late",
"2009 Early","Late", "2010 Early","Late","2011 Early","Late","2012 Early","Late", "2013 Early","Late"))
dt + theme(axis.text.x  = element_text(angle=90, vjust=0.5, size=10)) + ylab('Stem Density')

为了使事情进一步复杂化,我缺少某些测量日期的数据,但是想要在图中保留间隙,或者使用垂直线分隔一致测量的周期。

任何建议都表示赞赏!

根据要求输出dput:

   > dput(nzsum)
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L), .Label = c("2005 Early", 
"2005 Late", "2006 Early", "2006 Late", "2007 Early", "2007 Late", 
"2008 Early", "2008 Late", "2009 Early", "2009 Late", "2010 Early", 
"2010 Late", "2011 Early", "2011 Late", "2012 Early", "2012 Late", 
"2013 Early", "2013 Late"), class = "factor"), Treatment = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Ambient Precipitation", 
"Increased Precipitation Variability"), class = "factor"), Heat = structure(c(1L, 
1L,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 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("A. gerardii", 
"S. nutans"), class = "factor"), N = c(6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6), Average.Stem.Density = c(59.75, 
52.4166666666667, 59.4166666666667, 38.8333333333333, 47.1666666666667, 
44.6666666666667, 34.6666666666667, 47.1666666666667, 45.5833333333333, 
51, 46.8333333333333, 40.8333333333333, 36.0833333333333, 43.5, 
30.9166666666667, 53.1666666666667, 59.9166666666667, 47.8333333333333, 
54.8333333333333, 40.1666666666667, 45.0833333333333, 38.5833333333333, 
38.6666666666667, 38.3333333333333, 41.5, 57.3333333333333, 41.4166666666667, 
50.25, 32.8333333333333, 41.5833333333333, 29.5, 41.25, 57.75, 
34, 49.4166666666667, 45.1666666666667, 36.5833333333333, 22.6666666666667, 
33.0833333333333, 30.1666666666667, 37.8333333333333, 43.9166666666667, 
34.3333333333333, 44.4166666666667, 30.6666666666667, 31.1666666666667, 
28.0833333333333, 41.0833333333333, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 52.8333333333333, 43.5, 39, 
38.4166666666667, 40.5833333333333, 28.8333333333333, 33.3333333333333, 
26.6666666666667, 34.5833333333333, 53.3333333333333, 24.6666666666667, 
38.8333333333333, 26.9166666666667, 35.3333333333333, 22.1666666666667, 
16.3333333333333, 51.0833333333333, 58.25, 34.8333333333333, 
46.5833333333333, 47.5, 40, 35.4166666666667, 29.0833333333333, 
24.5, 62, 20.1666666666667, 40.6666666666667, 22.9166666666667, 
50, 20.25, 23.25, 32, 45.1666666666667, 30, 36.5, 31.6666666666667, 
36.9166666666667, 21.6666666666667, 18.4166666666667, NA, NA, 
NA, NA, NA, NA, NA, NA, 27.9166666666667, 29.75, 24.8333333333333, 
20.0833333333333, 19, 21.8333333333333, 15.5833333333333, 8.16666666666667, 
25, 42.25, 20.5833333333333, 29.25, 19.8333333333333, 23.1666666666667, 
19.4166666666667, 13.8333333333333, 46.6666666666667, 35, 36.4166666666667, 
29.4166666666667, 36, 22.6666666666667, 29.0833333333333, 18.5833333333333, 
33.3333333333333, 31.25, 25.8333333333333, 28.3333333333333, 
23.1666666666667, 14.0833333333333, 18.4166666666667, 15.3333333333333
), sd = c(32.2020962050609, 15.1407287363147, 25.7884017858158, 
14.1833235409289, 13.7501515143167, 22.0333081189972, 11.0574258607809, 
23.6107320231006, 25.6542718989775, 13.2815661727072, 15.4099534933324, 
11.2590704175197, 8.15730756234351, 15.52417469626, 11.8423674434915, 
20.9300422041301, 36.3612660212302, 14.7738507731284, 16.7022952514517, 
14.9788740119766, 7.61194237147567, 13.6872812006865, 12.9794709702155, 
16.7022952514517, 20.7797978815964, 13.47095641247, 10.4566565720916, 
18.5654248537436, 7.85281265959316, 14.756072196444, 9.46572765295939, 
20.3168649156311, 37.9983552275622, 15.3068612066615, 18.5240834231189, 
16.9813623324711, 8.59893404246519, 9.99833319442129, 9.61985793380893, 
16.0831174424198, 24.0409373084052, 17.8841177212259, 10.5340717040785, 
13.9191115616862, 8.09732466106347, 12.5445871461227, 9.43618920256831, 
21.0081333456037, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 32.7744819435284, 25.4872517153184, 6.09918027279076, 
26.1962147392837, 13.5033946349304, 13.6479546697176, 10.1176413588675, 
19.7855165883195, 13.9298839430437, 28.6856526275186, 6.8239773348588, 
23.1984194863932, 12.654709268358, 18.4842275106824, 4.79235502302017, 
13.1288486420808, 25.7864628568299, 31.8680874857592, 6.17791766428355, 
26.5582692709195, 19.2041662146525, 19.0446842977247, 18.5941298980798, 
18.0205900754295, 11.6790410565251, 28.8946361804401, 4.34357763446985, 
23.2350310235787, 8.88491230495083, 25.7740179250345, 9.23985930628816, 
11.5617905187735, 20.2410474037289, 22.2994768249541, 9.18694726228468, 
17.0117606378646, 17.229819112999, 20.4093524313406, 17.1571170849495, 
11.9558214551183, NA, NA, NA, NA, NA, NA, NA, NA, 15.0180447018467, 
17.2880016196205, 11.8939760663399, 10.3605823517149, 9.92975326984513, 
18.1457065628943, 9.86618805145466, 7.92254167970524, 17.5499287747842, 
14.7300712829232, 8.01508993503296, 15.5298100439123, 12.3193614553136, 
15.25013661141, 12.7766062264854, 10.7780641428165, 23.0079696337305, 
13.9319776054945, 10.9060380829459, 15.5609018590397, 21.0214176496258, 
14.0558410159857, 23.8189350447636, 15.6314320094695, 16.2777967387072, 
16.3240619944914, 6.17791766428355, 14.4798710859823, 14.770466027403, 
12.076492316342, 13.8759384067048, 12.6912043032435), se = c(13.146450725069, 
6.18117662297757, 10.5280709428545, 5.79031758868007, 5.6134758493389, 
8.99506037284415, 4.51417520459472, 9.63904098503119, 10.4733126458527, 
5.42217668469038, 6.29108716978058, 4.59649625016466, 3.33020686711468, 
6.33771778061052, 4.8346262638503, 8.54465394917261, 14.844424692269, 
6.03139932169789, 6.81868348322786, 6.11509970846302, 3.10756246026439, 
5.58780915127844, 5.29884683471581, 6.81868348322786, 8.48331696134635, 
5.49949492630409, 4.26891216952412, 7.57930295827613, 3.20589734361189, 
6.02414124815959, 3.86436713231718, 8.29432536939161, 15.5127635620908, 
6.2489999199872, 7.56242538989826, 6.93261214197874, 3.51050012264793, 
4.08180243411059, 3.92729055598273, 6.5659052011974, 9.81467155730531, 
7.30116048614496, 4.30051676481379, 5.68245349983419, 3.30571895021004, 
5.12130625697954, 3.85230811044207, 8.57653452417959, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 13.3801262242842, 
10.4051269414009, 2.48997991959775, 10.6945598839368, 5.51273777516923, 
5.57175416224051, 4.13050978828414, 8.07740332312584, 5.68685130610761, 
11.7108686460247, 2.78587708112026, 9.47071509678358, 5.16626342512437, 
7.54615428178118, 1.95647074544389, 5.35983001388829, 10.5272793784107, 
13.010092236414, 2.52212432507026, 10.8423680275318, 7.84006802691575, 
7.77495980697007, 7.5910217435541, 7.35687509144413, 4.76794854558366, 
11.79618582424, 1.77325814376939, 9.48566169425084, 3.62725025941752, 
10.5221987562803, 3.77215676591876, 4.72008121399057, 8.26337299977033, 
9.10372329202606, 3.75055551440939, 6.94502219818867, 7.03404419788345, 
8.33208323956927, 7.00436371921898, 4.88094367014321, NA, NA, 
NA, NA, NA, NA, NA, NA, 6.1310910756388, 7.05779710674655, 4.85569539590137, 
4.22969003329769, 4.05380479714223, 7.40795368356051, 4.02785440540136, 
3.23436409686836, 7.16472842006823, 6.01352641966426, 3.27214676389132, 
6.34001840165574, 5.02935825373819, 6.22584220094849, 5.21602764989263, 
4.4001262608147, 9.39296427001497, 5.68770604022395, 4.45237140309646, 
6.35271158202893, 8.58195781858662, 5.73827306580802, 9.72403951269453, 
6.38150539536802, 6.64538269109546, 6.66427040267725, 2.52212432507026, 
5.9113826169894, 6.03001750504185, 4.93020734294659, 5.66482813311911, 
5.18116246072679), ci = c(33.7940274369502, 15.889220349886, 
27.0632679299923, 14.8844852160264, 14.4298990530937, 23.1225388048852, 
11.604056783811, 24.7779436690751, 26.922507246369, 13.9381488952758, 
16.1717544050426, 11.8156697682437, 8.56056928151632, 16.2916222062267, 
12.4278024559438, 21.9647322335412, 38.1588084744178, 15.5042055398258, 
17.5279839049389, 15.7193642336799, 7.98824361346093, 14.3639207052787, 
13.6211194231394, 17.5279839049389, 21.8070604867824, 14.1369017627314, 
10.9735880809055, 19.4832185113292, 8.24102147840344, 15.4855480678265, 
9.93367195656454, 21.3212421334155, 39.8768282332316, 16.0635656852119, 
19.4398333406273, 17.8208468456823, 9.02402784927781, 10.4926071937808, 
10.0954217664752, 16.878196644708, 25.2294164279454, 18.7682305247498, 
11.0548302794798, 14.6072117485218, 8.49762108712868, 13.1647368389222, 
9.90267325397684, 22.0466838605638, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 34.3947094306661, 26.7472303130552, 
6.40069715241714, 27.4912413777728, 14.1709435894762, 14.3226500420865, 
10.6178134336812, 20.7636262615357, 14.6185166694449, 30.1037462209939, 
7.16132502104324, 24.3452481982785, 13.280302918737, 19.3980071256559, 
5.02926816019191, 13.7778816757996, 27.0612331488113, 33.4435067828789, 
6.48332697724211, 27.8711943068573, 20.1535364601427, 19.9861704525997, 
19.5133426079005, 18.9114494771115, 12.2564019245259, 30.3230610095818, 
4.55830517426775, 24.3836696502324, 9.32414363016577, 27.0481729938491, 
9.69663766364338, 12.1333550314124, 21.2416765342972, 23.4018657311414, 
9.64110987888638, 17.8527479107548, 18.0815862461422, 21.4183018286465, 
18.0052901468143, 12.5468651392342, NA, NA, NA, NA, NA, NA, NA, 
NA, 15.760471351669, 18.1426450422092, 12.4819623840869, 10.872764369967, 
10.4206369767492, 19.0427511781958, 10.3539293711124, 8.31419759724407, 
18.4175207338946, 15.458261782508, 8.41132103479517, 16.2975361408962, 
12.928376971967, 16.0040368732962, 13.4082259309908, 11.3108846405566, 
24.1453833352814, 14.6207138334386, 11.4451850543063, 16.3301649998, 
22.0606248826558, 14.7507005108872, 24.9964393403423, 16.4041818533482, 
17.0825000365819, 17.1310524448908, 6.48332697724211, 15.1956927787292, 
15.5006534670296, 12.6735014416993, 14.5619043009976, 13.318602109025
)), .Names = c("Date", "Treatment", "Heat", "Species", "N", "Average.Stem.Density", 
"sd", "se", "ci"), row.names = c(NA, -144L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

您可以根据需要自定义刻度线和标签。例如:

# Set tick marks on y axis
# a tick mark is shown on every 5
p + scale_y_continuous(breaks=seq(0,40,5))

# Tick marks can be spaced randomly
p + scale_y_continuous(breaks=c(5,7.5, 20, 25))

# Change x and y axis labels, and limits
sp + scale_x_continuous(name="Speed of cars", limits=c(0, 30)) +
  scale_y_continuous(name="Stopping distance", limits=c(0, 150))

您可以找到完整的详细信息和大量示例here

要解决您的第二个问题,我建议您gap.plot plotrix

twogrp<-c(rnorm(5)+4,rnorm(5)+20,rnorm(5)+5,rnorm(5)+22)
 gpcol<-c(2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
 gap.plot(twogrp,gap=c(8,16),xlab="Index",ylab="Group values",
  main="Gap on Y axis",col=gpcol)
 gap.plot(twogrp,rnorm(20),gap=c(8,16),gap.axis="x",xlab="X values",
  xtics=c(4,7,17,20),ylab="Y values",main="Gap on X axis with added lines")
 gap.plot(c(seq(3.5,7.5,by=0.5),seq(16.5,22.5,by=0.5)),
  rnorm(22),gap=c(8,16),gap.axis="x",type="l",add=TRUE,col=2,)
 gap.plot(twogrp,gap=c(8,16,25,35),
 xlab="X values",ylab="Y values",xlim=c(1,30),ylim=c(0,42),
 main="Test two gap plot with the lot",xtics=seq(0,30,by=5),
 ytics=c(4,6,18,20,22,38,40,42),
 lty=c(rep(1,10),rep(2,10)),
 pch=c(rep(2,10),rep(3,10)),
 col=c(rep(2,10),rep(3,10)),
 type="b")
 gap.plot(21:30,rnorm(10)+40,gap=c(8,16,25,35),add=TRUE,
  lty=rep(3,10),col=rep(4,10),type="l")

您还可以将axis.break与手动子集和数据重新缩放相结合。通过重新缩放,我的意思是在省略了您不想要的任何数据范围后,从具有更大值的观察值中减去该范围的大小,然后添加轴断点之字形并更改刻度线标记以反映数据在重新缩放之前。