Problem with ggplot and geom_rect, missing values for no reason

start = as_date(c("2018-07-01", "2020-01-01","2020-03-29", "2021-04-01"))
end = as_date(c("2018-07-07", "2020-01-07","2020-04-05", "2021-04-07"))
name = c("Bonus Malus", "WLTP", "Corona", "Höjd Malus")

weightedmonth <- ggplot(data = sum)+
  geom_col(                       
    mapping = aes(
      x = mon,
      y = weightedmean),
    fill = "NA",
    width = 0)+
  geom_line(                      
    mapping = aes(
      x = mon,
      y = weightedmean),
    size =1)+
  geom_rect(aes(xmin = start,
                           xmax = end,
                           ymin = 1, ymax = 200,
                           fill = name),
                       data = events,
                       show.legend = F) +
  geom_text(aes(x = start,
                y = 200,
                label = name,
                angle = 90),
            data = events)

weightedmonth +ylim(50,300)  
structure(list(mon = structure(c(15248, 15279, 15309, 15340, 
15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614, 
15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887, 
15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 
16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 
16467, 16495, 16526, 16556, 16587, 16617, 16648, 16679, 16709, 
16740, 16770, 16801, 16832, 16861, 16892, 16922, 16953, 16983, 
17014, 17045, 17075, 17106, 17136, 17167, 17198, 17226, 17257, 
17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501, 17532, 
17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805, 
17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 
18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 
18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 
18659, 18687, 18718, 18779, 18809, 18840, 18871, 18901, 18932, 
18962, 18993, 19024), class = "Date"), weightedmean = c(100, 
99.2423134414479, 98.3162838463254, 99.252112863364, 99.5180176702187, 
100.265674724215, 99.5525612633625, 102.407857383055, 104.046093697699, 
104.66047258202, 102.598193136023, 100.89554908301, 99.0737210660954, 
99.7234343618667, 100.236457519604, 101.235313994651, 108.011771697815, 
103.236771660583, 100.555880183775, 102.252606349836, 103.310819805765, 
103.273309437187, 101.467328426967, 101.590038465991, 103.733754890145, 
101.758699313206, 103.736957095661, 102.831338674159, 102.107090830357, 
103.858323214723, 102.992560502432, 101.362633485982, 101.431405853365, 
102.202327327611, 101.864204912544, 102.990800776382, 104.127963341819, 
104.947474998033, 105.126505109219, 104.291559527613, 107.481918483106, 
105.798025483102, 105.107722734023, 105.064767767392, 104.387176775546, 
101.68381801207, 102.910984756401, 102.663284891351, 102.710740535808, 
102.045790880458, 103.3525070335, 103.422952595365, 103.77251140577, 
102.109216613406, 103.494197806984, 105.225756237521, 106.177860726304, 
107.005917514352, 105.145538107452, 105.634822011824, 106.190723716057, 
106.178329381594, 100.154617659433, 102.970986917297, 103.897636611613, 
102.673533333473, 104.687709579375, 105.042104266581, 105.39060714005, 
106.725355476153, 104.931321526247, 105.242561881912, 105.55639736534, 
105.521151160823, 106.316235703397, 108.28397755938, 106.785912490329, 
106.253597258379, 106.950244960296, 105.576827797675, 102.79860029215, 
102.056101093817, 106.881141897409, 101.164750257887, 103.98014687603, 
105.377824820397, 104.465670462291, 107.413434331653, 108.413837193392, 
109.325721704088, 110.026216113471, 108.721791672861, 106.605098040895, 
110.994881506019, 110.622265127032, 114.737031043255, 111.35641331701, 
110.277652749156, 112.30506090765, 113.098391088588, 111.716085146746, 
112.246608039447, 110.601254989196, 116.237596880005, 110.052883652355, 
113.216036897086, 111.588134564165, 110.202745017972, 113.666813981058, 
112.60292327424, 117.435793934056, 116.597884503039, 117.240640510848, 
114.732062770459, 115.453828331953, 112.537171569202, 109.123726160378, 
108.631626040805, 110.773122832586, 113.845273630606, 118.408061884194, 
118.610968738796, 119.173005520999, 118.481490078228)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -124L))

Warning messages:
1: Removed 124 rows containing missing values (geom_col).
2: Removed 4 rows containing missing values (geom_rect).

The warning from geom_col I dont care about, but why wont geom_rect work?

[Solved] R + ggplot : Time series with events - Local Coder
The solution 4 is what I'm looking for, but with a line from each data (7days) each.

Or do anyone have a different solution?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.