使用R中的plot()和lines()连接平均点的置换线

时间:2019-04-01 10:59:43

标签: r plot line

这是我用来创建本文中描述的绘图的数据集:

> dput(act.byHour)
structure(list(hour = 0:23, `6/19/2018` = c(88L, 43L, 693L, 4297L, 
3708L, 1728L, 2528L, 2995L, 1321L, 830L, 2626L, 694L, 742L, 216L, 
3363L, 4420L, 843L, 199L, 200L, 85L, 0L, 685L, 696L, 1359L), 
    `6/20/2018` = c(59L, 74L, 2683L, 4341L, 3676L, 2143L, 3890L, 
    3887L, 1299L, 1492L, 3449L, 2200L, 1563L, 4346L, 5329L, 3037L, 
    1462L, 668L, 383L, 483L, 288L, 2765L, 3354L, 1783L), `6/21/2018` = c(241L, 
    301L, 261L, 3683L, 4356L, 3736L, 2810L, 1841L, 3146L, 609L, 
    2998L, 4059L, 3690L, 3735L, 1343L, 2087L, 894L, 341L, 240L, 
    2113L, 1684L, 3115L, 2890L, 138L), `6/22/2018` = c(21L, 451L, 
    96L, 2918L, 2279L, 2282L, 4992L, 698L, 427L, 581L, 1248L, 
    2184L, 1980L, 2364L, 568L, 2477L, 525L, 433L, 974L, 501L, 
    760L, 67L, 297L, 1198L), `6/23/2018` = c(2L, 39L, 42L, 1182L, 
    1749L, 2144L, 3123L, 1170L, 1641L, 1112L, 1526L, 1199L, 534L, 
    1481L, 2388L, 2756L, 392L, 112L, 390L, 107L, 709L, 1122L, 
    1562L, 451L), `6/24/2018` = c(8L, 74L, 0L, 158L, 780L, 3118L, 
    3292L, 2759L, 3121L, 2051L, 2387L, 900L, 627L, 904L, 4283L, 
    3726L, 1273L, 977L, 326L, 163L, 1915L, 1073L, 1021L, 545L
    ), `6/25/2018` = c(36L, 22L, 3L, 55L, 124L, 22L, 4093L, 2867L, 
    3649L, 2550L, 1590L, 636L, 2571L, 998L, 1066L, 2967L, 1211L, 
    51L, 1188L, 1413L, 714L, 177L, 132L, 29L), `6/26/2018` = c(22L, 
    43L, 0L, 90L, 1094L, 1655L, 2643L, 2108L, 2249L, 2453L, 2857L, 
    915L, 437L, 1142L, 2193L, 2993L, 1139L, 1549L, 652L, 580L, 
    970L, 674L, 211L, 206L), `6/27/2018` = c(167L, 63L, 1L, 786L, 
    617L, 1575L, 2237L, 1302L, 1149L, 2009L, 2234L, 1263L, 1259L, 
    2017L, 1641L, 2683L, 1184L, 449L, 65L, 956L, 1538L, 1287L, 
    593L, 362L), `6/28/2018` = c(594L, 1172L, 25L, 445L, 921L, 
    1812L, 2235L, 1153L, 422L, 1084L, 2158L, 1610L, 845L, 1187L, 
    2528L, 2161L, 976L, 19L, 747L, 570L, 576L, 19L, 304L, 2L), 
    `6/29/2018` = c(301L, 7L, 399L, 494L, 723L, 1088L, 771L, 
    85L, 1338L, 866L, 384L, 1356L, 2862L, 3805L, 2142L, 1655L, 
    249L, 235L, 3L, 0L, 283L, 981L, 634L, 1370L), `6/30/2018` = c(9L, 
    137L, 33L, 975L, 1690L, 1639L, 985L, 210L, 1266L, 2135L, 
    2080L, 1704L, 2449L, 3133L, 1055L, 3222L, 1152L, 173L, 858L, 
    188L, 700L, 330L, 905L, 1232L), `7/1/2018` = c(1006L, 5L, 
    21L, 520L, 1162L, 1771L, 2463L, 1403L, 1353L, 1938L, 2388L, 
    4133L, 900L, 2660L, 3504L, 3946L, 1956L, 818L, 604L, 937L, 
    373L, 48L, 400L, 201L), `7/10/2018` = c(705L, 47L, 605L, 
    257L, 1359L, 41L, 1019L, 1426L, 2219L, 1179L, 1624L, 537L, 
    421L, 1747L, 2941L, 2921L, 1046L, 283L, 476L, 218L, 59L, 
    389L, 657L, 1293L), `7/11/2018` = c(24L, 455L, 6L, 1232L, 
    2264L, 1152L, 600L, 11L, 980L, 1519L, 2004L, 1933L, 2161L, 
    1386L, 1883L, 2978L, 1385L, 104L, 1309L, 2L, 364L, 550L, 
    0L, 1433L), `7/12/2018` = c(1634L, 27L, 860L, 1095L, 1102L, 
    132L, 582L, 710L, 1368L, 2470L, 2944L, 1030L, 1286L, 387L, 
    2590L, 2449L, 743L, 134L, 274L, 205L, 360L, 627L, 1357L, 
    591L), `7/13/2018` = c(216L, 143L, 70L, 2L, 477L, 42L, 81L, 
    304L, 2827L, 2437L, 2002L, 688L, 935L, 812L, 404L, 1098L, 
    1157L, 857L, 466L, 215L, 714L, 269L, 1223L, 8L), `7/14/2018` = c(1L, 
    635L, 6L, 1797L, 1363L, 246L, 704L, 1089L, 943L, 2251L, 813L, 
    2643L, 1657L, 18L, 1132L, 2884L, 1044L, 149L, 1146L, 68L, 
    1227L, 1189L, 129L, 1291L), `7/15/2018` = c(7L, 9L, 1299L, 
    389L, 288L, 157L, 0L, 324L, 248L, 915L, 795L, 598L, 733L, 
    308L, 2760L, 2874L, 1903L, 499L, 73L, 31L, 1146L, 920L, 852L, 
    2L), `7/16/2018` = c(104L, 564L, 16L, 1903L, 675L, 1859L, 
    720L, 1017L, 4L, 2114L, 2264L, 1152L, 935L, 1691L, 1031L, 
    2568L, 2035L, 226L, 18L, 1716L, 249L, 717L, 635L, 919L), 
    `7/17/2018` = c(1436L, 16L, 17L, 1891L, 1175L, 74L, 435L, 
    377L, 718L, 619L, 439L, 1373L, 2154L, 2481L, 763L, 2084L, 
    910L, 641L, 669L, 737L, 793L, 1471L, 12L, 96L), `7/18/2018` = c(6L, 
    13L, 81L, 1227L, 1685L, 260L, 238L, 575L, 930L, 330L, 1139L, 
    785L, 1110L, 1007L, 1770L, 2824L, 729L, 776L, 602L, 550L, 
    1432L, 567L, 197L, 107L), `7/19/2018` = c(38L, 648L, 264L, 
    911L, 2239L, 1063L, 9L, 1336L, 1235L, 628L, 1722L, 1028L, 
    1393L, 44L, 2110L, 1719L, 666L, 127L, 885L, 788L, 1274L, 
    765L, 1094L, 38L), `7/2/2018` = c(876L, 505L, 162L, 775L, 
    1567L, 896L, 1648L, 995L, 2574L, 1080L, 997L, 1881L, 1375L, 
    1283L, 2156L, 2384L, 982L, 33L, 20L, 761L, 241L, 696L, 133L, 
    915L), `7/20/2018` = c(514L, 14L, 59L, 1081L, 1266L, 359L, 
    1055L, 280L, 123L, 2251L, 2302L, 1116L, 2750L, 764L, 1377L, 
    2776L, 970L, 814L, 10L, 1364L, 1137L, 279L, 10L, 605L), `7/21/2018` = c(279L, 
    596L, 12L, 1443L, 1463L, 1426L, 132L, 924L, 379L, 693L, 137L, 
    219L, 884L, 194L, 450L, 1204L, 487L, 578L, 445L, 9L, 823L, 
    2L, 1212L, 12L), `7/22/2018` = c(200L, 9L, 152L, 1062L, 1926L, 
    1156L, 1951L, 1735L, 753L, 570L, 362L, 813L, 756L, 1403L, 
    308L, 1895L, 325L, 768L, 666L, 33L, 634L, 1294L, 819L, 39L
    ), `7/23/2018` = c(579L, 8L, 657L, 438L, 521L, 896L, 2560L, 
    1383L, 819L, 1293L, 2257L, 476L, 1850L, 759L, 2482L, 1513L, 
    789L, 78L, 329L, 43L, 50L, 1583L, 342L, 0L), `7/24/2018` = c(495L, 
    13L, 127L, 1415L, 1534L, 939L, 2315L, 649L, 154L, 2838L, 
    1462L, 2255L, 1058L, 316L, 1825L, 2391L, 324L, 185L, 813L, 
    997L, 830L, 407L, 796L, 624L), `7/25/2018` = c(1002L, 6L, 
    86L, 1091L, 1951L, 8L, 1863L, 2555L, 799L, 749L, 2386L, 1893L, 
    524L, 846L, 2263L, 2266L, 779L, 88L, 380L, 1495L, 1985L, 
    3L, 1462L, 1450L), `7/26/2018` = c(1L, 19L, 967L, 1565L, 
    1066L, 9L, 99L, 4L, 1889L, 1848L, 1924L, 471L, 1357L, 626L, 
    1465L, 1787L, 1437L, 115L, 322L, 717L, 1639L, 990L, 1029L, 
    1112L), `7/27/2018` = c(372L, 8L, 256L, 1679L, 1209L, 2246L, 
    2153L, 1762L, 1883L, 1551L, 998L, 728L, 1274L, 888L, 508L, 
    2357L, 452L, 1167L, 2385L, 3280L, 320L, 1130L, 878L, 583L
    ), `7/28/2018` = c(799L, 4L, 61L, 394L, 1237L, 854L, 68L, 
    379L, 2910L, 3088L, 1011L, 840L, 1024L, 2496L, 3079L, 2830L, 
    1841L, 1772L, 595L, 65L, 584L, 2110L, 1966L, 473L), `7/29/2018` = c(21L, 
    847L, 293L, 881L, 840L, 1912L, 683L, 1362L, 1276L, 3131L, 
    3110L, 1773L, 1077L, 1437L, 769L, 2311L, 1623L, 562L, 42L, 
    1791L, 1318L, 1230L, 202L, 2630L), `7/3/2018` = c(623L, 918L, 
    48L, 523L, 721L, 1624L, 1047L, 1783L, 313L, 1042L, 2211L, 
    2430L, 1770L, 1610L, 2814L, 2460L, 1770L, 25L, 709L, 416L, 
    709L, 998L, 921L, 89L), `7/30/2018` = c(1174L, 396L, 52L, 
    2261L, 1237L, 56L, 927L, 2491L, 3180L, 352L, 81L, 2072L, 
    3207L, 2394L, 600L, 3280L, 1745L, 147L, 1L, 1544L, 350L, 
    2198L, 1833L, 55L), `7/4/2018` = c(258L, 1242L, 75L, 1131L, 
    893L, 402L, 381L, 51L, 15L, 47L, 762L, 777L, 479L, 2416L, 
    3639L, 1991L, 202L, 1054L, 917L, 1565L, 503L, 61L, 44L, 2103L
    ), `7/5/2018` = c(2212L, 352L, 1L, 666L, 351L, 1321L, 7L, 
    1010L, 1222L, 1080L, 1643L, 1101L, 188L, 2793L, 1548L, 1811L, 
    1807L, 51L, 788L, 1108L, 1157L, 1038L, 225L, 454L), `7/6/2018` = c(441L, 
    376L, 444L, 5L, 501L, 579L, 1253L, 1600L, 1051L, 498L, 2217L, 
    2362L, 2425L, 1220L, 2037L, 2684L, 799L, 471L, 139L, 545L, 
    1117L, 177L, 487L, 1420L), `7/7/2018` = c(692L, 303L, 736L, 
    750L, 1386L, 926L, 30L, 862L, 1912L, 2731L, 1123L, 1160L, 
    2892L, 1634L, 585L, 3473L, 2243L, 441L, 399L, 1482L, 111L, 
    455L, 1315L, 691L), `7/8/2018` = c(1428L, 96L, 52L, 258L, 
    1135L, 1727L, 448L, 2148L, 358L, 2180L, 1519L, 2634L, 828L, 
    1212L, 1052L, 2851L, 902L, 171L, 236L, 3L, 727L, 1366L, 637L, 
    43L), `7/9/2018` = c(0L, 1320L, 146L, 664L, 862L, 663L, 227L, 
    227L, 995L, 743L, 1793L, 2421L, 1346L, 1874L, 2182L, 1333L, 
    1967L, 1023L, 297L, 340L, 1469L, 10L, 213L, 805L)), class = "data.frame", row.names = c(NA, 
-24L))

这是我过去每天plot每小时activity平均hour所使用的代码。我还添加了sd栏和连接这些点的线:

mean <- rowMeans(act.byHour[,-1],na.rm=T)
act.byHour<-as.matrix(act.byHour)
sd<-rowSds(act.byHour[,-1],na.rm=T)
act.byHour<-as.data.frame(act.byHour)
plot(main="activity collar 41361", 
     act.byHour$hour,rowMeans(act.byHour[,-1],na.rm=T),
     type="p",pch=19,xlab="Hour of the day",ylab="Activity",ylim = c(-100, 3200))
lines(mean, col="darkgray")

arrows(act.byHour$hour,mean-sd,act.byHour$hour,mean+sd, code=3, length=0.02, angle = 90)

这是情节:itself

如您所见,连接点的线在x轴上偏移+1。有人可以告诉我使用lines()时是否做错了吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您未提供lines语句的x值。因此,它将默认使用1:x范围内的数字索引。由于您的数据以0开头,因此将移位1。

为您的lines语句提供与您的plot语句相同的x值,并且效果很好:

lines(act.byHour$hour,mean, col="darkgray")