4

跟进之前提出的问题(使用 ggplot 在“时间序列”中绘制深度范围),但有点完全相同。

使用 ggplot 我想要一个图来显示特定个体(例如 7303,参见下面的代码示例)深度范围如何在 3mnd 周期内围绕平均值(最小值和最大值)变化,使用geom_ribbon().

数据集中的变量;

names(df)
[1] "ID"    "MONTH" "DAY"   "MIN"   "MEAN"  "MAX"   "date" 

我如何选择一个人:

df<-Dybde_dag_gjsn[which(Dybde_dag_gjsn$ID=="7303"),]

我尝试了以下方法;

ggplot(data=df)+
    geom_line(aes(x=date,y=-MEAN,group=ID,colour=as.factor(ID))) +
    geom_ribbon(aes(x=date,ymin=-MIN,ymax=-MAX,group=ID),fill="grey") +
    theme(legend.position="none")

但结果并不令人满意,只显示“垂直手段”而不是介于两者之间的手段:

http://dl.dropbox.com/u/85946387/fail_7303.jpeg

我认为这与我的grouping factor(参考:书“ggplot2:用于数据分析的优雅图形(使用 R!)”第 50 页底部)有关。我使用 ID 作为差异图的分组因素,其中我在同一个图中有很多人,但当然这在这里不起作用,因为我现在只有一个ID。

我该如何解决这个问题?!我一直在阅读和阅读 ggplot 但一切都是徒劳的..

下面是它的代码示例:

 > dput(df)
    structure(list(ID = c(7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 7303L, 
    7303L, 7303L, 7303L, 7303L, 7303L, 7303L), MONTH = c(6L, 7L, 
    8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 
    8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 
    8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 6L, 7L, 
    8L, 9L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 12L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
    5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    12L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 5L, 6L, 7L, 8L, 9L, 10L, 
    11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
    5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 
    7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 
    8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 
    9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 
    10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 6L, 7L, 8L, 9L, 10L, 
    11L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 5L, 7L, 8L, 10L), DAY = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    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, 15L, 
    15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 
    18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 
    20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 
    22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 
    24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 
    26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L, 
    28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 
    30L, 30L, 30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L), MIN = c(11.802917, 
    8.604167, 14.318936, 14.836458, 10.16625, 8.58093, 12.620833, 
    11.216667, 9.742083, 16.554167, 14.662708, 11.58375, 8.401957, 
    17.662553, 11.33413, 8.3275, 16.664167, 13.108, 12.163542, 8.024783, 
    16.394255, 12.450625, 7.730208, 17.397083, 14.680833, 12.08875, 
    8.810435, 15.019792, 10.742292, 11.911489, 17.621111, 14.555319, 
    11.092292, 10.339583, 15.487083, 9.9375, 12.747708, 16.411458, 
    13.903542, 10.94875, 10.164375, 16.915625, 7.065417, 12.83375, 
    18.342979, 13.549167, 11.229792, 10.56875, 18.430444, 6.761042, 
    12.088723, 18.041489, 12.976458, 10.995625, 9.83, 17.677083, 
    9.044737, 8.485208, 8.480833, 18.887609, 13.97125, 10.979583, 
    9.73, 12.843913, 12.495833, 9.311042, 11.773958, 17.697447, 12.004583, 
    9.507083, 8.385417, 16.856596, 13.131458, 8.656458, 10.16625, 
    17.634255, 14.123542, 9.138958, 9.999583, 18.634043, 12.649583, 
    8.672917, 13.166458, 17.596222, 13.121875, 8.397708, 10.421667, 
    20.087805, 14.496042, 10.430833, 10.553333, 17.742979, 12.378125, 
    8.736458, 8.713125, 19.968421, 13.571875, 11.819792, 8.847021, 
    18.748085, 14.197917, 8.918125, 9.3475, 11.85625, 11.089583, 
    13.461458, 18.761277, 11.001702, 8.621458, 9.194375, 11.738333, 
    12.283404, 15.249583, 17.522609, 11.655625, 8.725957, 9.024792, 
    12.361875, 12.311702, 11.959167, 16.179333, 11.733542, 11.0825, 
    11.635208, 12.139792, 10.24875, 11.147917, 15.690851, 11.7175, 
    8.537708, 8.896458, 11.342917, 9.42875, 12.289167, 13.904894, 
    13.622766, 9.262292, 10.925417, 12.507917, 9.640417, 11.619167, 
    13.693333, 12.81375, 8.737021, 8.725217, 12.851042, 9.978958, 
    11.987917, 12.68875, 13.178125, 8.613125, 8.759574, 13.125833, 
    12.803191, 10.768085, 14.104583, 12.426042, 8.590208, 10.004792, 
    11.335417, 10.126458, 11.02, 13.675625, 12.153333, 8.786667, 
    8.901522, 10.190208, 9.675417, 12.787708, 12.376875, 12.686596, 
    9.266042, 9.486875, 11.187083, 11.984583, 14.908125, 12.341042, 
    8.603617, 8.950417, 9.289574, 12.193125, 11.034583, 14.760417, 
    14.270417, 10.900426, 9.456667, 9.817333, 11.300625, 9.979362, 
    18.275532, 14.270833, 10.328333, 8.991667, 9.73234, 10.31625, 
    12.294167, 17.291875, 14.166889, 10.655625, 9.542917, 9.423542, 
    13.408542, 11.412708, 15.77875, 13.872174, 10.414583, 8.771458, 
    10.196667, 13.245957, 9.7525, 14.025625, 14.0225, 10.283333, 
    10.124375, 11.077708, 11.284375, 12.960625, 13.672917, 8.689535
    ), MEAN = c(12.770743, 10.404807, 15.731308, 15.718022, 11.039786, 
    9.592478, 13.566293, 11.997254, 11.060343, 17.321561, 15.434626, 
    12.521116, 9.173196, 20.231528, 12.183485, 9.494385, 17.661506, 
    13.833904, 13.130883, 9.006861, 17.73971, 13.204396, 8.896995, 
    18.11753, 15.600875, 13.121007, 10.101822, 16.769785, 11.697461, 
    12.764369, 18.282592, 15.310545, 12.133705, 11.40675, 16.982279, 
    11.110351, 13.55874, 17.101128, 14.624049, 11.851105, 11.142173, 
    18.371564, 8.477442, 13.410731, 18.981083, 14.22986, 12.254827, 
    11.94385, 19.470508, 8.074123, 12.675037, 18.886745, 13.915082, 
    12.136526, 11.31768, 18.839881, 10.007904, 9.772924, 9.81347, 
    19.389361, 14.811928, 12.119235, 10.951059, 14.149285, 13.293671, 
    10.514356, 12.565706, 18.040379, 12.948702, 10.605055, 9.573924, 
    18.062445, 14.017553, 9.602413, 11.364352, 18.259258, 15.170781, 
    9.967036, 11.390614, 19.553701, 13.97901, 9.462041, 13.8245, 
    18.099865, 14.177431, 9.98118, 11.685662, 20.804762, 15.019522, 
    11.354073, 11.534092, 18.394013, 13.326405, 9.616245, 9.949249, 
    20.426667, 14.46714, 12.737376, 9.717531, 19.227388, 15.078364, 
    9.844263, 10.303065, 12.92404, 12.265582, 14.105216, 19.288396, 
    12.052717, 9.491347, 10.529289, 13.096898, 13.369252, 16.28977, 
    18.160727, 12.549057, 9.604447, 10.280789, 13.554763, 13.393719, 
    12.50955, 16.820558, 12.850228, 12.094866, 12.630164, 13.385122, 
    11.273304, 11.961472, 16.330921, 12.611663, 9.617874, 10.167019, 
    12.655299, 10.373309, 13.374857, 14.925013, 14.537989, 10.447603, 
    12.508138, 13.349818, 10.680128, 12.326957, 15.049259, 13.660166, 
    9.696145, 9.914241, 13.980217, 10.761571, 12.556846, 13.99646, 
    14.085468, 9.438403, 9.728357, 14.105513, 13.989112, 11.698711, 
    14.95036, 13.363272, 9.401711, 10.865912, 12.682327, 11.131757, 
    11.846839, 14.42893, 13.07079, 9.819241, 9.933585, 11.365231, 
    10.918715, 14.062566, 13.084255, 13.71815, 10.182433, 10.690687, 
    12.208993, 13.50174, 15.4533, 13.201188, 9.811705, 9.881868, 
    10.245386, 12.971602, 11.964325, 15.679961, 15.016042, 11.940247, 
    10.361256, 10.998961, 12.15253, 11.044938, 19.022599, 14.954921, 
    11.48031, 10.01924, 10.652463, 11.569661, 13.298019, 17.929547, 
    15.186122, 11.539514, 10.618905, 10.354405, 14.389687, 12.624194, 
    16.338045, 14.791213, 11.211347, 9.772962, 12.016292, 14.260883, 
    10.820083, 14.909629, 14.889066, 11.19011, 11.194178, 12.819223, 
    12.179042, 14.309247, 14.722921, 9.69601), MAX = c(13.891875, 
    12.046875, 16.884468, 16.663333, 12.111875, 10.590465, 14.557708, 
    12.835833, 12.286458, 18.15125, 16.4475, 13.609167, 9.929783, 
    22.586596, 13.212826, 10.684792, 18.319792, 14.831333, 14.148958, 
    10.042826, 19.206383, 14.106667, 9.974375, 18.710625, 16.462917, 
    14.298958, 11.42913, 18.739583, 12.672708, 13.598723, 18.842222, 
    16.099149, 13.169167, 12.736458, 18.593125, 12.367708, 14.195833, 
    17.82625, 15.53375, 12.754375, 12.278125, 20.19, 9.94375, 13.917292, 
    19.738511, 14.916458, 13.339375, 13.453542, 20.28, 9.341042, 
    13.397234, 19.54, 14.982083, 13.249792, 12.602083, 20.058333, 
    10.945789, 11.00875, 10.901875, 19.990217, 15.745, 13.379583, 
    12.361042, 15.389565, 14.012917, 11.743958, 13.204167, 18.383404, 
    13.976667, 11.698958, 10.67125, 19.185106, 14.927083, 10.43, 
    12.549167, 18.929149, 16.060833, 10.792708, 12.95, 20.521277, 
    15.096667, 10.184792, 14.455208, 18.589778, 15.085208, 11.300208, 
    13.256875, 21.602439, 15.5625, 12.430625, 12.52375, 18.934043, 
    14.175417, 10.624792, 11.299583, 20.831579, 15.508333, 13.814792, 
    10.412766, 19.688511, 15.974583, 10.839583, 11.752083, 14.33625, 
    13.332708, 14.671667, 19.667447, 12.983404, 10.354583, 11.980833, 
    14.325833, 14.310426, 17.290833, 18.684783, 13.448958, 10.530851, 
    11.582708, 14.854375, 14.357234, 13.0875, 17.491111, 14.056458, 
    12.958125, 13.423333, 14.718542, 12.291458, 12.82125, 16.907872, 
    13.553125, 11.034375, 11.5625, 13.86375, 11.493542, 14.3025, 
    15.835957, 15.34383, 11.731667, 14.293125, 14.225625, 11.83875, 
    12.958125, 16.238333, 14.615625, 10.958085, 11.225435, 14.90375, 
    11.537083, 13.103958, 15.006458, 14.866458, 10.4675, 10.701702, 
    15.096875, 15.155319, 12.481702, 15.745208, 14.3425, 10.37875, 
    11.7625, 13.89875, 12.302708, 12.761875, 15.10625, 14.171875, 
    10.985625, 10.838696, 12.592708, 12.229167, 15.030833, 13.759792, 
    14.696809, 11.215, 12.2125, 13.277292, 14.844375, 15.933542, 
    13.974583, 11.086809, 10.918542, 11.214894, 13.733542, 12.78375, 
    16.320625, 15.781667, 12.809362, 11.3175, 12.139778, 13.163958, 
    11.853617, 19.77383, 15.644375, 12.645625, 11.055417, 11.401702, 
    12.80125, 14.210625, 18.491667, 15.958444, 12.377083, 11.693333, 
    11.346667, 15.291458, 13.693542, 16.73125, 15.604565, 12.090833, 
    10.700417, 13.640208, 15.333404, 11.697292, 15.565, 15.66375, 
    12.119583, 12.334167, 14.415833, 13.394792, 15.494583, 15.681042, 
    10.907907), date = structure(c(15522, 15553, 15492, 15522, 15553, 
    15492, 15522, 15554, 15493, 15523, 15554, 15493, 15523, 15554, 
    15494, 15524, 15555, 15494, 15524, 15555, 15494, 15525, 15556, 
    15495, 15525, 15556, 15495, 15525, 15557, 15496, 15526, 15557, 
    15496, 15526, 15557, 15497, 15527, 15558, 15497, 15527, 15558, 
    15497, 15528, 15559, 15498, 15528, 15559, 15498, 15528, 15560, 
    15499, 15529, 15560, NA, 15499, 15499, 15500, 15500, 15500, 15530, 
    15500, 15500, 15500, 15561, 15501, 15531, 15501, 15501, 15531, 
    15501, 15531, 15562, 15532, 15502, 15532, 15502, 15532, 15532, 
    15502, 15532, 15533, 15564, 15533, 15564, 15564, 15564, 15564, 
    15564, 15565, 15565, 15565, 15565, 15534, 15534, 15534, 15504, 
    15505, 15505, 15535, 15505, 15535, 15505, 15505, 15536, 15506, 
    15506, 15536, 15506, 15506, 15506, 15537, 15507, 15568, 15568, 
    15568, 15568, 15568, 15569, 15569, 15569, 15569, 15569, 15569, 
    15569, 15570, 15570, 15570, 15570, 15570, 15539, 15570, 15571, 
    15540, 15571, 15540, 15540, 15571, 15540, 15572, 15541, 15511, 
    15541, 15511, 15541, 15541, 15542, 15512, 15512, 15512, 15573, 
    15512, 15512, 15513, 15513, 15513, 15574, 15513, 15543, 15574, 
    15514, 15544, 15514, 15514, 15514, 15514, 15575, 15545, 15576, 
    15515, 15545, 15515, 15576, 15576, 15546, 15577, 15546, 15516, 
    15546, 15577, 15546, 15578, 15517, 15517, 15547, 15578, 15547, 
    15547, 15579, 15518, 15579, 15518, 15579, 15548, 15548, 15580, 
    15580, 15519, 15549, 15519, 15549, 15519, 15520, 15520, 15581, 
    15550, 15581, 15550, 15520, 15521, 15551, 15582, 15582, 15582, 
    15521, 15551, 15583, 15552, 15583, 15583), class = "Date")), .Names = c("ID", 
    "MONTH", "DAY", "MIN", "MEAN", "MAX", "date"), row.names = 1128:1346, class = "data.frame")

我很感激所有的指点!干杯

更新:问题解决了,一切都很顺利;

在此处输入图像描述

4

1 回答 1

4

一个问题是日期不是唯一的,这导致了那些垂直线。由于该列的月份date与该MONTH列不同,因此数据似乎有问题。此外,您需要切换 和 的顺序geom_linegeom_ribbon以便在顶部绘制线条。

您可以按如下方式重新创建日期:

df$date2 <- as.Date(paste("2012",df$MONTH,df$DAY,sep="-"),"%Y-%m-%d")

ggplot(data=df)+  
  geom_ribbon(aes(x=date2,ymin=-MIN,ymax=-MAX),fill="grey") +
  geom_line(aes(x=date2,y=-MEAN)) +
  theme(legend.position="none")

在此处输入图像描述

于 2013-03-30T15:09:04.553 回答