2017-04-07 67 views
0

我很感激我的問題的任何幫助,鱗片不對齊。並排地塊,但Ÿ即使相同

我並排繪製兩個圖表的一面。它們都具有相同的y尺度,但它們不是對齊的(一個小區中的50個比另一個小,但150個處於相同的水平)。我該如何糾正這一點?

require(cowplot) 
    p3 = ggplot(ECGHR_merged) +geom_line(aes(x = time, y=ECG_HR_ch, group=id), alpha=0.2) + 
     geom_hline(aes(yintercept = 70.66), color="blue") + 
     scale_y_continuous(limits=c(20,190)) + 
     ggtitle("ECG HR: Challenge Day") + xlab("Time before reaction (Mins)") + 
     ylab("ECG HR (BPM)") 



p4 = ggplot(ECGHR_merged) +geom_line(aes(x = time, y=ECG_HR_pl, group=id), alpha=0.2)+ 
    geom_hline(aes(yintercept = 67.09), color="blue") + 
    ggtitle("ECG HR: Placebo Day") + xlab("Time before reaction (Mins.)")+ 
    ylab("ECG HR (BPM)") 


plot_grid(p4, p3) 

enter image description here

示例數據:

print(ECGHR_merged) 
      id time T.x ECG_HR_ch T.y ECG_HR_pl 
1 2003 0 T0  70 T0  57 
2 2003 1 T1  65 T1  66 
44 2003 2 T2  69 T2  56 
55 2003 3 T3  71 T3  62 
66 2003 4 T4  68 T4  59 
77 2003 5 T5  74 T5  58 
88 2003 6 T6  67 T6  71 
99 2003 7 T7  65 T7  68 
110 2003 8 T8  60 T8  66 
121 2003 9 T9  66 T9  61 
3 2003 10 T10  59 T10  61 
14 2003 11 T11  99 T11  59 
25 2003 12 T12  74 T12  59 
36 2003 13 T13  73 T13  62 
38 2003 14 T14  87 T14  62 
39 2003 15 T15  94 T15  65 
40 2003 16 T16  88 T16  65 
41 2003 17 T17  88 T17  67 
42 2003 18 T18  98 T18  63 
43 2003 19 T19  73 T19  64 
45 2003 20 T20  72 T20  65 
46 2003 21 T21  63 T21  63 
47 2003 22 T22  79 T22  58 
48 2003 23 T23  63 T23  65 
49 2003 24 T24  62 T24  60 
50 2003 25 T25  66 T25  70 
51 2003 26 T26  57 T26  63 
52 2003 27 T27  64 T27  65 
53 2003 28 T28  70 T28  61 
54 2003 29 T29  65 T29  64 
56 2003 30 T30  78 T30  59 
57 2003 31 T31  59 T31  73 
58 2003 32 T32  64 T32  71 
59 2003 33 T33  69 T33  66 
60 2003 34 T34  60 T34  54 
61 2003 35 T35  55 T35  59 
62 2003 36 T36  61 T36  55 
63 2003 37 T37  70 T37  59 
64 2003 38 T38  71 T38  61 
65 2003 39 T39  64 T39  58 
67 2003 40 T40  56 T40  64 
68 2003 41 T41  68 T41  60 
69 2003 42 T42  64 T42  62 
70 2003 43 T43  63 T43  65 
71 2003 44 T44  65 T44  58 
72 2003 45 T45  67 T45  64 
73 2003 46 T46  59 T46  64 
74 2003 47 T47  58 T47  60 
75 2003 48 T48  60 T48  60 
76 2003 49 T49  61 T49  66 
78 2003 50 T50  63 T50  58 
79 2003 51 T51  65 T51  63 
80 2003 52 T52  67 T52  56 
81 2003 53 T53  60 T53  61 
82 2003 54 T54  67 T54  57 
83 2003 55 T55  62 T55  54 
84 2003 56 T56  64 T56  59 
85 2003 57 T57  60 T57  61 
86 2003 58 T58  58 T58  57 
87 2003 59 T59  64 T59  61 
89 2003 60 T60  65 T60  65 
90 2003 61 T61  64 T61  63 
91 2003 62 T62  63 T62  60 
92 2003 63 T63  63 T63  56 
93 2003 64 T64  63 T64  58 
94 2003 65 T65  69 T65  57 
95 2003 66 T66  63 T66  64 
96 2003 67 T67  62 T67  59 
97 2003 68 T68  71 T68  59 
98 2003 69 T69  61 T69  57 
100 2003 70 T70  68 T70  55 
101 2003 71 T71  63 T71  60 
102 2003 72 T72  68 T72  56 
103 2003 73 T73  60 T73  58 
104 2003 74 T74  60 T74  65 
105 2003 75 T75  61 T75  58 
106 2003 76 T76  65 T76  60 
107 2003 77 T77  59 T77  60 
108 2003 78 T78  66 T78  66 
109 2003 79 T79  61 T79  60 
111 2003 80 T80  58 T80  62 
112 2003 81 T81  59 T81  63 
113 2003 82 T82  57 T82  59 
114 2003 83 T83  59 T83  58 
115 2003 84 T84  60 T84  61 
116 2003 85 T85  58 T85  57 
117 2003 86 T86  63 T86  65 
118 2003 87 T87  64 T87  64 
119 2003 88 T88  61 T88  63 
120 2003 89 T89  58 T89  64 
122 2003 90 T90  65 T90  61 
123 2003 91 T91  60 T91  56 
124 2003 92 T92  63 T92  60 
125 2003 93 T93  55 T93  62 
126 2003 94 T94  57 T94  59 
127 2003 95 T95  61 T95  59 
128 2003 96 T96  57 T96  56 
129 2003 97 T97  72 T97  59 
130 2003 98 T98  55 T98  62 
131 2003 99 T99  73 T99  56 
4 2003 100 T100  56 T100  85 
5 2003 101 T101  59 T101  56 
6 2003 102 T102  61 T102  55 
7 2003 103 T103  63 T103  55 
8 2003 104 T104  57 T104  59 
9 2003 105 T105  60 T105  61 
10 2003 106 T106  61 T106  55 
11 2003 107 T107  56 T107  57 
12 2003 108 T108  62 T108  57 
13 2003 109 T109  60 T109  65 
15 2003 110 T110  63 T110  55 
16 2003 111 T111  60 T111  57 
17 2003 112 T112  57 T112  55 
18 2003 113 T113  69 T113  79 
19 2003 114 T114  65 T114  59 
20 2003 115 T115  60 T115  59 
21 2003 116 T116  61 T116  58 
22 2003 117 T117  62 T117  61 
23 2003 118 T118  65 T118  61 
24 2003 119 T119  59 T119  63 
26 2003 120 T120  60 T120  68 
27 2003 121 T121  60 T121  60 
28 2003 122 T122  72 T122  61 
29 2003 123 T123  67 T123  62 
30 2003 124 T124  63 T124  65 
31 2003 125 T125  59 T125  64 
32 2003 126 T126  62 T126  58 
33 2003 127 T127  58 T127  61 
34 2003 128 T128  64 T128  58 
35 2003 129 T129  60 T129  58 
37 2003 130 T130  68 T130  60 
132 2004 0 T0  98 T0  77 
133 2004 1 T1  105 T1  82 
175 2004 2 T2  85 T2  69 
186 2004 3 T3  87 T3  88 
197 2004 4 T4  83 T4  100 
208 2004 5 T5  83 T5  91 
219 2004 6 T6  77 T6  72 
230 2004 7 T7  71 T7  79 
241 2004 8 T8  75 T8  65 
252 2004 9 T9  73 T9  73 
134 2004 10 T10  73 T10  69 
145 2004 11 T11  71 T11  65 
156 2004 12 T12  87 T12  63 
167 2004 13 T13  74 T13  62 
169 2004 14 T14  79 T14  64 
170 2004 15 T15  75 T15  63 
171 2004 16 T16  72 T16  64 
172 2004 17 T17  71 T17  67 
173 2004 18 T18  71 T18  69 
174 2004 19 T19  68 T19  66 
176 2004 20 T20  86 T20  65 
177 2004 21 T21  72 T21  64 
178 2004 22 T22  73 T22  69 
179 2004 23 T23  69 T23  67 
180 2004 24 T24  75 T24  66 
181 2004 25 T25  73 T25  68 
182 2004 26 T26  70 T26  68 
183 2004 27 T27  76 T27  67 
184 2004 28 T28  71 T28  64 
185 2004 29 T29  68 T29  69 
187 2004 30 T30  68 T30  67 
188 2004 31 T31  76 T31  65 
189 2004 32 T32  69 T32  65 
190 2004 33 T33  73 T33  66 
191 2004 34 T34  74 T34  64 

回答

2

您設置p3scale_y_continuous(limits=c(20,190)),但不是在p4

這意味着p4中的限制是根據其中的數據計算的。

require(cowplot) 

p3 <- ggplot(ECGHR_merged) + 
    geom_line(aes(x = time, y = ECG_HR_ch, group = id), alpha = 0.2) + 
    geom_hline(aes(yintercept = 70.66), color = "blue") + 
    scale_y_continuous(limits = c(20,190)) + 
    ggtitle("ECG HR: Challenge Day") + 
    xlab("Time before reaction (Mins)") + 
    ylab("ECG HR (BPM)") 

p4 <- ggplot(ECGHR_merged) + 
    geom_line(aes(x = time, y = ECG_HR_pl, group = id), alpha=0.2)+ 
    geom_hline(aes(yintercept = 67.09), color = "blue") + 
    scale_y_continuous(limits = c(20,190)) + 
    ggtitle("ECG HR: Placebo Day") + 
    xlab("Time before reaction (Mins.)")+ 
    ylab("ECG HR (BPM)") 

plot_grid(p4, p3) 

+0

完美!非常感謝。 –

+0

喜@sara_khan請因此它得到應有的信用標記答案接受 – pacomet