Reputation: 306
I'm trying to plot a stock price and some color above it as an indicator. The problem is that the color should not always be present which is indicated by having missing values, but using geom_ribbon
causes the missing values to appear. The code is as follows:
library(dplyr)
library(ggplot2)
library(lubridate)
library(padr)
# Does not work correctly
ggplot(x, aes(x = Date, y = Stock)) +
geom_ribbon(data = a, inherit.aes = FALSE, aes(
x = Date2,
ymin = Price * 1.1,
ymax = Price,
y = Price,
fill = Group
), alpha = 0.9) +
geom_line()
# Add missing values as NAs
b <- pad(a)
# Works but does not show colors
ggplot(x, aes(x = Date, y = Stock)) +
geom_ribbon(data = b,
inherit.aes = FALSE,
aes(x = Date2,
ymin = Price * 1.1,
ymax = Price,
y = Price#,
#fill = Group
),
alpha = 0.9) +
geom_line()
# Does not work correctly because of the grouping caused by the fill
ggplot(x, aes(x = Date, y = Stock)) +
geom_ribbon(data = b,
inherit.aes = FALSE,
aes(x = Date2,
ymin = Price * 1.1,
ymax = Price,
y = Price,
fill = Group),
alpha = 0.9) +
geom_line()
It should look like this but without the missing values being filled in:
Padding the missing values with NAs allows to not show the missing values. Only the following points should have a ribbon above them, but it seems impossible to color them:
If the ribbons are colored after padding, then the data get grouped and the missing values get interpolated, which should not be the case:
Data:
x <- structure(list(Date = structure(c(18213, 18214, 18215, 18218,
18219, 18220, 18221, 18222, 18225, 18226, 18227, 18229, 18232,
18233, 18234, 18235, 18236, 18239, 18240, 18241, 18242, 18243,
18246, 18247, 18248, 18249, 18250, 18253, 18254, 18256, 18257,
18260, 18261, 18263, 18264, 18267, 18268, 18269, 18270, 18271,
18274, 18275, 18276, 18277, 18278, 18282, 18283, 18284, 18285,
18288, 18289, 18290, 18291, 18292, 18295, 18296, 18297, 18298,
18299, 18302, 18303, 18304, 18305, 18306, 18310, 18311, 18312,
18313, 18316, 18317, 18318, 18319, 18320, 18324, 18325, 18326,
18327, 18328, 18331, 18332, 18333, 18334, 18335, 18338, 18339,
18340, 18341, 18342, 18345, 18346, 18347, 18348, 18349, 18352,
18353, 18354, 18355, 18356, 18359, 18360, 18361, 18362, 18363,
18366, 18367, 18368, 18369, 18373, 18374, 18375, 18376, 18377,
18380, 18381, 18382, 18383, 18384, 18387, 18388, 18389, 18390,
18391, 18394, 18395, 18396, 18397, 18398, 18401, 18402, 18403,
18404, 18405, 18409, 18410, 18411, 18412, 18415, 18416, 18417,
18418, 18419, 18422, 18423, 18424, 18425, 18426, 18429, 18430,
18431, 18432, 18433, 18436, 18437, 18438, 18439, 18440, 18443,
18444, 18445, 18446, 18450, 18451, 18452, 18453, 18454, 18457,
18458, 18459, 18460, 18461, 18464, 18465, 18466, 18467, 18468,
18471, 18472, 18473, 18474, 18475, 18478, 18479, 18480, 18481,
18482, 18485, 18486, 18487, 18488, 18489, 18492, 18493, 18494,
18495, 18496, 18499, 18500, 18501, 18502, 18503, 18507, 18508,
18509, 18510, 18513, 18514, 18515, 18516, 18517, 18520, 18521,
18522, 18523, 18524, 18527, 18528, 18529, 18530, 18531, 18534,
18535, 18536, 18537, 18538, 18541, 18542, 18543, 18544, 18545,
18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557, 18558,
18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570, 18571,
18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583, 18584,
18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597, 18598,
18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608, 18611,
18612, 18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625,
18626, 18627), class = "Date"),
Stock = c(32.302406, 32.783638,
32.753559, 33.084404, 32.723484, 32.377602, 32.663322, 32.994171,
32.422722, 32.332485, 32.106918, 32.00164, 31.445225, 31.294838,
31.249725, 30.738419, 31.580574, 31.881338, 31.941496, 31.460262,
31.370037, 31.430185, 31.685837, 31.730949, 32.531425, 32.305305,
32.320374, 32.893227, 32.817848, 32.516354, 32.697247, 32.531425,
33.013821, 31.883207, 31.702311, 31.084244, 31.219913, 31.129463,
30.963646, 31.536489, 31.023943, 31.38574, 31.476185, 31.219913,
31.250069, 31.280209, 31.415892, 30.692297, 30.119457, 30.07423,
29.938557, 29.305418, 29.606909, 29.395864, 28.265259, 29.124521,
29.471237, 30.05916, 30.104383, 29.938557, 30.330505, 30.360651,
30.119457, 30.360651, 29.998857, 28.958696, 29.486313, 29.712437,
29.727509, 29.245119, 28.732574, 28.476297, 27.767788, 25.627169,
26.727627, 26.622101, 26.079411, 25.883436, 25.868364, 25.20507,
24.858349, 23.652367, 24.662378, 25.551794, 26.667328, 25.868364,
25.159845, 25.597015, 25.597015, 26.652252, 26.652252, 26.727627,
26.546728, 27.828083, 28.024061, 27.767788, 27.390923, 27.481363,
26.893454, 27.044199, 27.285393, 26.953745, 27.225088, 26.365831,
25.627169, 26.184935, 27.601965, 26.652252, 26.411057, 26.395979,
25.702539, 24.978949, 26.184935, 26.124634, 26.139708, 26.139708,
25.732691, 25.883436, 26.380905, 27.737633, 27.782866, 27.586891,
27.933609, 28.325556, 28.702429, 28.370781, 28.642126, 29.395864,
29.682287, 30.013933, 29.727509, 30.044086, 29.410936, 29.471237,
29.712437, 29.214966, 29.426016, 29.772738, 29.848104, 29.953634,
30.345573, 30.436029, 30.541553, 30.782749, 30.390808, 30.601849,
30.194832, 30.209909, 30.963646, 30.707371, 30.586777, 30.888273,
31.491264, 31.226501, 30.954172, 31.075211, 30.606213, 30.454916,
31.271893, 31.302149, 31.196245, 31.211376, 31.710632, 31.150858,
31.105465, 30.863405, 30.575953, 30.969309, 28.89662, 29.501785,
28.836103, 29.123556, 28.972263, 29.093298, 28.639423, 28.20068,
27.610645, 28.351974, 27.882971, 27.928352, 28.125036, 27.232416,
26.521349, 26.808798, 27.550123, 27.958612, 27.746809, 28.246065,
28.397356, 28.291456, 28.881491, 29.138687, 29.108423, 29.516909,
28.972263, 29.486652, 29.380754, 29.229462, 28.321712, 27.686293,
27.338318, 29.00252, 28.715067, 29.380754, 29.00252, 28.730202,
29.032782, 28.20068, 28.185551, 27.535, 27.988873, 27.580385,
27.429092, 27.277803, 26.823933, 27.247543, 27.988873, 27.262672,
27.171898, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
26.521349, 25.825409, 26.11286, 25.47744, 25.053823, 25.295889,
25.250502, 25.462311, 24.887403, 25.386665, 25.053823, 25.295889,
24.660463, 24.509178, 24.963047, 24.645338, 25.038694, 24.766371,
25.961571, 25.537954, 25.159723, 25.038694, 24.811756, 23.782978,
24.176332, 24.645338, 24.478916, 24.61508, 23.767849, 23.979656,
23.934263, 24.040173, 24.085558, 24.5243, 24.947918, 25.038694,
24.312494, 23.979656, 22.618032, 22.723938, 21.649773, 22.118773,
21.558996, 21.649773, 20.409182, 20.424313, 19.985567, 18.835754,
18.260849, 16.173027, 16.309193, 18.383097, 17.293257, 19.008156,
19.745405, 19.376781, 19.825541, 20.130056, 18.206802, 19.55308,
18.383097)),
class = "data.frame", row.names = c(NA, -285L))
a <- structure(list(Price = c(16.173027, 16.309193, 25.627169, 26.079411,
25.883436, 25.868364, 25.20507, 24.858349, 23.652367, 24.662378,
25.551794, 25.868364, 25.159845, 25.597015, 25.597015, 26.365831,
25.627169, 26.184935, 26.411057, 26.395979, 25.702539, 24.978949,
26.184935, 26.124634, 26.139708, 26.139708, 25.732691, 25.883436,
26.380905, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
25.825409, 26.11286, 25.47744, 25.053823, 25.295889, 25.250502,
25.462311, 24.887403, 25.386665, 25.053823, 25.295889, 24.660463,
24.509178, 24.963047, 24.645338, 25.038694, 24.766371, 25.961571,
25.537954, 25.159723, 25.038694, 24.811756, 23.782978, 24.176332,
24.645338, 24.478916, 24.61508, 23.767849, 23.979656, 23.934263,
24.040173, 24.085558, 24.5243, 24.947918, 25.038694, 24.312494,
23.979656, 22.618032, 22.723938, 21.649773, 22.118773, 21.558996,
21.649773, 20.409182, 20.424313, 19.985567, 18.835754, 18.260849,
18.383097, 17.293257, 19.008156, 19.745405, 19.376781, 19.825541,
20.130056, 18.206802, 19.55308, 18.383097),
Date2 = structure(c(18611,
18612, 18324, 18327, 18328, 18331, 18332, 18333, 18334, 18335,
18338, 18340, 18341, 18342, 18345, 18366, 18367, 18368, 18374,
18375, 18376, 18377, 18380, 18381, 18382, 18383, 18384, 18387,
18388, 18534, 18535, 18536, 18537, 18538, 18542, 18543, 18544,
18545, 18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557,
18558, 18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570,
18571, 18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583,
18584, 18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597,
18598, 18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608,
18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625, 18626,
18627), class = "Date"),
Group = c("b", "b", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a")),
class = "data.frame", row.names = c(NA,
-92L))
Upvotes: 0
Views: 228
Reputation: 306
Now that I understood the logic of teunbrand's answer, I came up with a much clearer solution:
b <- a %>%
mutate(run = as.character(
cumsum(ifelse(Date2 - lag(Date2) <= 1 | is.na(lag(Date2)),
ifelse(lag(Group) == Group | is.na(lag(Date2)), 0, 1), 1))))
Upvotes: 0
Reputation: 37903
You can set the group independently from the fill. The only downside is that you have to calculate these groups yourself. You can do this with run length encoding.
# Calculate Rle
rle <- rle(b$Group)
# Replace non-NA runs with run IDs
rle <- within(unclass(rle), values[!is.na(values)] <- seq_along(values[!is.na(values)]))
# Add Rle to data
b$run <- inverse.rle(rle)
# Discard unecessary data
b <- b[!is.na(b$Group),]
ggplot(x, aes(x = Date, y = Stock)) +
geom_ribbon(data = b, inherit.aes = FALSE, aes(
x = Date2,
ymin = Price * 1.1,
ymax = Price,
y = Price,
fill = Group,
group = run
), alpha = 0.9) +
geom_line()
Upvotes: 1