如何针对三个时间点中的每一个绘制一个变量

时间:2017-04-13 00:09:51

标签: r plot

我有一个数据集,其中包含一个名为Soma(Somatotype)的DV和三个称为WT2(2岁时体重),WT9(9岁时体重),WT18(体重18岁)的数据集,我将描绘体重对抗体重三个时间点中的每一个。但由于它不完全像时间序列数据集,我完全坚持这一点。 我在考虑使用ggplot,但我对此并不熟悉并且失败了很多。 数据集:

structure(list(X = 67:136, Sex = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
WT2 = c(13.6, 11.3, 17, 13.2, 13.3, 11.3, 11.6, 11.6, 12.4, 
17, 12.2, 15, 14.5, 10.2, 12.2, 12.8, 13.6, 10.9, 13.1, 13.4, 
11.8, 12.7, 11.8, 14.1, 10.9, 11.8, 13.6, 12.7, 12.3, 11.5, 
12.6, 14.1, 11.5, 12, 10.9, 12.7, 11.3, 11.8, 15.4, 10.9, 
13.2, 14.3, 11.1, 13.6, 12.9, 13.5, 16.3, 13.6, 10.2, 12.6, 
12.9, 13.3, 13.4, 12.7, 12.2, 15.4, 12.7, 13.2, 12.4, 10.9, 
13.4, 10.6, 11.8, 14.2, 12.7, 13.2, 11.8, 13.3, 13.2, 15.9
), HT2 = c(87.7, 90, 89.6, 90.3, 89.4, 85.5, 90.2, 82.2, 
85.6, 97.3, 87.1, 88.9, 87.6, 82.6, 87.1, 84, 83.6, 81.4, 
89.7, 88.4, 86.4, 83.8, 87.6, 94, 82, 86.4, 88.9, 86.7, 86.4, 
86.4, 83.8, 88.9, 85.9, 86.2, 85.1, 88.6, 83, 88.9, 89.7, 
81.3, 88.7, 88.4, 85.1, 91.4, 87.6, 86.1, 94, 85.9, 82.2, 
88.2, 87.5, 88.6, 86.9, 86.4, 80.9, 90, 94, 89.7, 86.4, 82.6, 
86.4, 81.8, 86.2, 86, 91.4, 88.9, 88.6, 86.4, 94, 89.2), 
WT9 = c(32.5, 27.8, 44.4, 40.5, 29.9, 22.8, 30, 24.3, 29.9, 
44.5, 31.8, 32.1, 39.2, 23.7, 26, 36.3, 29.9, 22.2, 34.4, 
35.5, 33, 25.7, 29.2, 31.7, 23.7, 35.3, 39, 30.8, 29.3, 28, 
33, 47.4, 27.6, 34.2, 28.1, 27.5, 23.9, 32.2, 29.4, 22, 28.8, 
38.8, 36, 31.3, 26.9, 33.3, 36.2, 29.5, 23.4, 33.8, 34.5, 
34.4, 38.2, 31.7, 26.6, 34.2, 27.7, 28.5, 30.5, 26.6, 39, 
25, 25.6, 34.2, 29.8, 27.9, 27, 41.4, 41.6, 42.4), HT9 = c(133.4, 
134.8, 141.5, 137.1, 136.1, 130.6, 136, 128, 132.4, 152.5, 
138.4, 135.2, 142.3, 129.1, 133.2, 136.3, 133.1, 123.2, 135.8, 
139.5, 139.4, 124.2, 135.6, 144.1, 123.8, 134.6, 137.2, 139.8, 
128.8, 134.2, 136.5, 140.8, 132.1, 137, 129, 139.4, 125.6, 
137.1, 133.6, 121.4, 133.6, 134.1, 139.4, 138.1, 133.2, 138.4, 
139.5, 132.8, 129.8, 144.8, 138.9, 140.3, 143.8, 133.6, 123.5, 
139.9, 136.1, 135.8, 131.9, 133.1, 130.9, 126.3, 135.9, 135, 
135.5, 136.5, 134, 138.2, 142, 140.8), LG9 = c(28.4, 26.9, 
31.9, 31.8, 27.7, 23.4, 27.2, 25.1, 27.5, 32.7, 28.3, 26.9, 
31.6, 25.9, 26.7, 28.4, 26.2, 24.9, 32.3, 30, 26.9, 26.2, 
26.3, 27.2, 25.5, 30.4, 32.4, 26, 28.3, 25, 29, 32.3, 26.3, 
27.3, 27.4, 25.7, 24.5, 28.2, 26.6, 24.4, 26.5, 31.1, 28.2, 
27.6, 26.3, 29.4, 28, 27.6, 22.6, 28.3, 30.5, 31.2, 29.8, 
27.5, 27.2, 29.1, 26.7, 25.5, 28.6, 25.4, 29.3, 25, 23.7, 
27.6, 27, 26.5, 26.5, 32.5, 31, 32.6), ST9 = c(74L, 65L, 
104L, 79L, 83L, 60L, 67L, 44L, 76L, 81L, 59L, 67L, 72L, 40L, 
40L, 54L, 67L, 58L, 57L, 61L, 64L, 48L, 61L, 74L, 50L, 58L, 
80L, 57L, 44L, 46L, 57L, 69L, 51L, 44L, 48L, 68L, 22L, 59L, 
58L, 44L, 58L, 57L, 64L, 64L, 58L, 73L, 52L, 52L, 60L, 107L, 
62L, 88L, 78L, 52L, 40L, 71L, 30L, 76L, 59L, 75L, 38L, 50L, 
45L, 62L, 57L, 66L, 54L, 44L, 56L, 74L), WT18 = c(56.9, 49.9, 
55.3, 65.9, 62.3, 47.4, 57.3, 50, 58.8, 80.2, 59.9, 56.3, 
67.9, 52.9, 58.5, 73.2, 54.7, 44.1, 70.5, 60.6, 73.2, 57.2, 
56.4, 56.6, 46.3, 63.3, 65.4, 60.1, 55, 55.7, 71.2, 65.5, 
57.2, 58.2, 56, 64.5, 53, 52.4, 56.8, 49.2, 55.6, 77.8, 69.6, 
56.2, 52.5, 64.9, 59.3, 54.2, 49.8, 62.6, 66.6, 65.3, 65.9, 
59, 47.4, 60.4, 56.3, 61.7, 52.4, 52.1, 58.4, 52.8, 60.4, 
61, 67.4, 54.3, 56.3, 97.7, 68.1, 63.1), HT18 = c(158.9, 
166, 162.2, 167.8, 170.9, 164.9, 168.1, 164, 163.3, 183.2, 
167, 163.8, 174, 163, 167.1, 168.1, 163, 154.6, 170.3, 170.6, 
175.1, 156.5, 160.3, 170.8, 156.5, 165.2, 169.8, 171.2, 160.4, 
163.8, 169.6, 172.7, 162.4, 166.8, 157.1, 181.1, 158.4, 165.6, 
166.7, 156.5, 168.1, 165.3, 163.7, 173.7, 163.9, 169.2, 170.1, 
166, 164.2, 176, 170.9, 169.2, 172, 163, 154.5, 172.5, 175.6, 
167.2, 164, 162.1, 161.6, 153.6, 177.5, 169.8, 173.5, 166.8, 
166.2, 162.8, 168.6, 169.2), LG18 = c(34.6, 33.8, 35.1, 39.3, 
36.3, 31.8, 35, 31.2, 36.2, 42.9, 36.5, 32.6, 37.5, 37.7, 
34.5, 37.2, 33.2, 32.4, 40.1, 38.2, 35.1, 35.6, 34.6, 32.6, 
32.9, 38.5, 38.6, 33, 36.3, 33.2, 38.8, 36.2, 36.5, 34.3, 
37.8, 34.2, 32.4, 33.8, 32.7, 33.5, 34.1, 39.8, 38.6, 34.2, 
34.6, 36.7, 32.8, 34.9, 30.3, 35.8, 38.8, 39, 35.7, 32.7, 
32.2, 35.7, 34, 35.5, 34.8, 34.1, 33, 33.4, 34.3, 34.5, 34.5, 
33.6, 36.2, 42.5, 38.4, 37.9), ST18 = c(143L, 117L, 143L, 
148L, 152L, 126L, 134L, 77L, 118L, 135L, 118L, 96L, 131L, 
108L, 99L, 105L, 122L, 146L, 126L, 124L, 100L, 118L, 123L, 
131L, 101L, 121L, 182L, 116L, 127L, 130L, 107L, 134L, 120L, 
130L, 101L, 149L, 112L, 136L, 118L, 110L, 104L, 138L, 108L, 
134L, 108L, 141L, 122L, 125L, 128L, 168L, 126L, 142L, 132L, 
116L, 112L, 137L, 114L, 122L, 121L, 148L, 107L, 140L, 125L, 
124L, 123L, 89L, 135L, 125L, 142L, 142L), Soma = c(5, 4, 
5.5, 5.5, 4.5, 3, 5, 4, 5, 5.5, 5, 5, 5.5, 4, 5, 6.5, 4.5, 
3.5, 5.5, 4.5, 6, 5, 4.5, 4, 4, 5, 4.5, 4.5, 5, 5, 6, 4.5, 
5, 5, 5, 4, 5, 4, 4.5, 4, 4.5, 6.5, 5.5, 3.5, 4, 5, 4.5, 
4, 4, 5, 5, 5, 5.5, 5.5, 4, 4, 3, 4.5, 5, 4, 6.5, 5, 3.5, 
5.5, 5, 4, 4.5, 7, 5.5, 5.5)), .Names = c("X", "Sex", "WT2", 
"HT2", "WT9", "HT9", "LG9", "ST9", "WT18", "HT18", "LG18", "ST18", 
"Soma"), row.names = 67:136, class = "data.frame")

我的命令:

library(tidyr)
library(ggplot2)
newdata.girls %>% 
# put WT2, WT9, WT18 in the weight column
# and the weights in the value column
gather(weight, value, -Soma) %>% 
# make WT2, WT9, WT18 factors and order them so as
# they plot in the correct order
mutate(weight = factor(weight, levels = c("WT2", "WT9", "WT18"))) %>%
# plot Soma versus value by time
ggplot(aes(Soma, value)) + geom_point() + facet_grid(. ~ weight)

它给出了一列NA。 result

1 个答案:

答案 0 :(得分:1)

您希望输出看起来如何,或者Soma是连续的还是绝对的,并不完全清楚。但是将你的句子“Soma在三个时间点中的每个时间点对抗体重”作为起点,初步尝试可能看起来像这样。假设您的数据框名为df1

library(tidyr)
library(dplyr)
library(ggplot2)
df1 %>% 
  # put WT2, WT9, WT18 in the weight column
  # and the weights in the value column
  gather(weight, value, -Soma) %>% 
  # make WT2, WT9, WT18 factors and order them so as
  # they plot in the correct order
  mutate(weight = factor(weight, levels = c("WT2", "WT9", "WT18"))) %>%
  # plot Soma versus value by time
  ggplot(aes(Soma, value)) + geom_point() + facet_grid(. ~ weight) + theme_light()

结果: enter image description here