设为首页收藏本站获取积分奖励

R语言中文网

 找回密码
 立即注册
查看: 2457|回复: 4

[问题求助] 关于lattice包作图求助

[复制链接]
发表于 2017-10-7 11:26:19 | 显示全部楼层 |阅读模式
求各位大神指导!!!!
本人想做一个类似图1的图,我的数据就一个变量(distance1),有一个随机效应变量(year),想看的是sum1~ distance1 的一个关系,部分数据如下:


idyeardistance1sum1 sum
1
2006
183.21
5148
3.711723
2
2006
442
1360
3.133858
3
2006
187.33
11455
4.059033
4
2006
795.74
287
2.459392
5
2006
684.69
1485
3.172019
6
2006
552.89
2382
3.377124
7
2006
813.09
553
2.74351
8
2006
525.11
53149
4.725503
9
2006
510.27
29680
4.472479
10
2006
806.29
201
2.305351
11
2006
728.41
207
2.318063
12
2006
770.91
42170
4.625014
13
2006
766.86
11507
4.061
14
2006
987.29
563
2.751279
15
2006
1250.55
703
2.847573
16
2006
1369.28
6
0.845098
17
2006
1246.47
4551
3.658202
18
2006
1157.04
2032
3.308137
19
2006
1178.97
5850
3.76723
20
2006
1435.69
49
1.69897
21
2006
1264.55
1392
3.143951
22
2006
1358.17
172
2.238046
23
2006
1579.72
216
2.33646
24
2006
1561.39
119
2.079181
25
2006
1509.79
0
0
26
2006
1563.55
52
1.724276
27
2006
1456.39
57
1.763428
28
2006
1472.95
161
2.209515
29
2006
1398.46
1920
3.283527
30
2006
1632.7
8
0.954243
31
2006
1590.86
3
0.60206
32
2006
902.86
705
2.848805
33
2006
902.35
90
1.959041
34
2007
442
38
1.591065
35
2007
187.33
4061
3.60874
36
2007
183.21
5148
3.711723
37
2007
525.11
3067
3.486855
38
2007
795.74
118
2.075547
39
2007
684.69
741
2.870404
40
2007
576.22
56
1.755875
41
2007
813.09
420
2.624282
42
2007
510.27
67498
4.829297
43
2007
551.74
921
2.964731
44
2007
552.89
0
0
45
2007
806.29
1086
3.03623
46
2007
770.91
104824
5.020465
47
2007
987.29
6
0.845098
48
2007
0.99
6892
3.838408
49
2007
1435.69
0
0
50
2007
1369.28
792
2.899273
51
2007
1246.47
4009
3.603144
52
2007
1250.55
8568
3.93293
53
2007
1157.04
327
2.515874
54
2007
1178.97
542
2.7348
55
2007
1358.17
340
2.532754
56
2007
1579.72
102
2.012837
57
2007
1561.39
114
2.060698
58
2007
1509.79
619
2.792392
59
2007
1632.7
735
2.866878
60
2007
1590.86
74
1.875061
61
2007
1442.58
357
2.553883
62
2007
1251.58
98
1.995635
63
2007
1456.39
36
1.568202
64
2007
1472.95
120
2.082785
65
2007
902.86
48
1.690196
66
2007
902.35
0
0
67
2008
183.21
3596
3.55594
68
2008
442
1040
3.017451
69
2008
187.33
17585
4.245167
70
2008
795.74
1131
3.053846
71
2008
684.69
734
2.866287
72
2008
510.27
24588
4.390741
73
2008
576.22
70
1.851258
74
2008
525.11
2545
3.405858
75
2008
552.89
129
2.113943
76
2008
806.29
512
2.710117
77
2008
813.09
172
2.238046
78
2008
728.41
1524
3.18327
79
2008
770.91
29491
4.469704
80
2008
900.81
369
2.568202


图1有个代码,我不太会改,对lattice包中的panel函数不太清楚怎么弄,所以请教各位大神,可以怎么改,能做出类似的图来
附上图1的代码和数据
图1 的代码
MyData <- expand.grid(Time = seq(1,4,length = 10),
                      Treatment = levels(KW$Treatment))
Z <- model.matrix(~Time + Treatment, data = MyData)
MyData$eta <- Z %*% Betas
MyData$mu  <- exp(MyData$eta)

xyplot(Dandelion ~ Time | Treatment,
       xlab = list("Time (days)", cex = 1.5),
       ylab = list("Number of dandelion pollen grains", cex = 1.5),
       data = KW, layout = c(3,1),
       type = "p", col = 1,
       strip = strip.custom(bg = 'white',
                            par.strip.text = list(cex = 1.2)),
       scales = list(alternating = T,
                     x = list(relation = "same"),
                     y = list(relation = "same")),

       panel = function(x,y, subscripts,...){
               ID <- KW$Treatment[subscripts][1]
               x1 <- MyData[MyData$Treatment == ID,"Time"]
               y1 <- MyData[MyData$Treatment == ID,"mu"]
               panel.lines(x1,y1, lwd = 5, col =1)
               panel.points(x,y, pch = 16, col = 1)
              
               xx1 <- KW$Time[subscripts]
               yy1 <- mu[subscripts]
               Hives <- KW$Hive[subscripts]
               NH <- unique(Hives)
               for (i in 1:4){
               panel.lines(xx1[Hives==NH],
                           yy1[Hives==NH],
                           lwd = 1, col = 1)
       }}            
)

p.s. 代码中Betas 、eta有一个很长的代码计算。。。就没放了。。。主要想知道后面xyplot那块针对我自己的数据可以怎么改。。。。
图1的数据:
Time        Hive        Treatment        Dandelion       
1        1        Syrup        236       
2        1        Syrup        167       
3        1        Syrup        227       
4        1        Syrup        225       
1        2        Syrup        306       
2        2        Syrup        308       
3        2        Syrup        241       
4        2        Syrup        304       
1        3        Syrup        36       
2        3        Syrup        10       
3        3        Syrup        2       
4        3        Syrup        19       
1        4        Syrup        249       
2        4        Syrup        208       
3        4        Syrup        66       
4        4        Syrup        43       
1        5        Syrup        139       
2        5        Syrup        145       
3        5        Syrup        146       
4        5        Syrup        110       
1        6        Protein        347       
2        6        Protein        140       
3        6        Protein        153       
4        6        Protein        38       
1        7        Protein        129       
2        7        Protein        43       
3        7        Protein        42       
4        7        Protein        39       
1        8        Protein        35       
2        8        Protein        89       
3        8        Protein        100       
4        8        Protein        27       
1        9        Protein        111       
2        9        Protein        87       
3        9        Protein        48       
4        9        Protein        25       
1        10        Protein        98       
2        10        Protein        76       
3        10        Protein        27       
4        10        Protein        59       
1        11        Control        32       
2        11        Control        50       
3        11        Control        9       
4        11        Control        31       
1        12        Control        13       
2        12        Control        1       
3        12        Control        0       
4        12        Control        6       
1        13        Control        230       
2        13        Control        222       
3        13        Control        227       
4        13        Control        225       
1        14        Control        31       
2        14        Control        51       
3        14        Control        11       
4        14        Control        29       
1        15        Control        11       
2        15        Control        2       
3        15        Control        1       
4        15        Control        7       



拜托了!!!!万分感谢!!!!
360截图164408187410070.png
回复

使用道具 举报

发表于 2017-10-11 09:59:05 | 显示全部楼层
MyData=read.csv("test_lattice_data.csv",header=TRUE)#test_lattice_data.csv保存了你的数据

MyPanel=function(x,y){
    panel.lines(x,y,lwd=5,col=1)
    panel.points(x,y,pch=16,col=1)
    panel.rug(x,y)
    panel.grid(-1,-1)
}

xyplot(sum1 ~ distance1 | year,
        xlab = list("Time (days)", cex = 1.5),
        ylab = list("Number of dandelion pollen grains", cex = 1.5),
        data = MyData, layout = c(3,1),
        type = "p", col = 1,
        panel = MyPanel )

你先运行上述代码试试,先画一个简单的,然后再拓展!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-19 16:29:16 | 显示全部楼层
chunfei_xu 发表于 2017-10-11 09:59
MyData=read.csv("test_lattice_data.csv",header=TRUE)#test_lattice_data.csv保存了你的数据

MyPanel= ...

谢谢,能把每一年的情况给画出来,但是如果我想把这些都放在一张图里面,该怎么操作?我看例子的代码有for循环,然后我可以怎么改?
回复 支持 反对

使用道具 举报

发表于 2017-10-19 17:36:52 | 显示全部楼层
把每年的数据分开画在一张图里吗?那就用不着那么复杂啦。常规画图就行了,何必一定要用这种格式呢。
方法就是按年筛选,然后用几次lines函数设置不同的颜色属性就能画出不同年份,不同颜色的曲线了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-19 20:01:20 | 显示全部楼层
chunfei_xu 发表于 2017-10-19 17:36
把每年的数据分开画在一张图里吗?那就用不着那么复杂啦。常规画图就行了,何必一定要用这种格式呢。
方法 ...

也对,自己太纠结了,哈哈哈
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Archiver|R语言中文网    

GMT+8, 2024-5-20 15:03 , Processed in 0.027539 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表