0

Here's the example data:

ex_data <- structure(list(variable = c("Act", "Act", "Act", "Act", "Act", 
"Act", "Act", "Act", "Act", "Act", "Act", "Act", "Act", "Act", 
"Act", "Act", "Act", "Act", "Act", "Act", "Act", "Act", "Act", 
"Act", "Act", "Act", "Act", "Act", "Act", "Act", "Act", "Act", 
"Act", "Act", "Act", "Act", "Act", "Act", "Proj", "Proj", "Proj", 
"Proj", "Proj", "Proj", "Proj", "Proj", "Proj", "Proj", "Proj", 
"Proj", "Proj", "Proj", "Proj", "Proj", "Proj", "Goal", "Goal", 
"Goal", "Goal", "Goal", "Goal", "Goal", "Goal", "Goal", "Goal", 
"Goal", "Goal", "Thresh", "Thresh", "Thresh", "Thresh", "Thresh", 
"Thresh", "Thresh", "Thresh", "Thresh", "Thresh", "Thresh", "Thresh"
), value = c(6.89658227475411, 6.89108850833475, 6.91060575775871, 
7.00129392659955, 7.1096850611615, 7.07369364373404, 7.06060119204421, 
7.21032922407943, 7.34249288006833, 7.39138104921065, 7.4318910161, 
7.5961207493889, 7.85673978348162, 7.92089086256755, 7.95756924344796, 
7.98326636757226, 8.0193147690931, 7.97898702366837, 8.08257378865854, 
8.07398725017176, 7.97609133959215, 8.09248918915805, 8.13257527841791, 
8.03078376104876, 7.88034051912236, 7.82481943232295, 7.72428815030338, 
7.55539008452296, 7.42128217313615, 7.45838229538017, 7.41767125677876, 
7.31265597647537, 7.35972091839823, 7.27265834882302, 7.15542963221048, 
7.1447264083441, 7.35752135743515, 7.59728236343906, 7.32707490147607, 
7.25109817340699, 7.20263208921773, 7.23082681383693, 7.24648885635746, 
7.3729353600057, 7.43036853995031, 7.53395376136055, 7.60535703635852, 
7.61387042389277, 7.60922305418595, 7.65197881774436, 7.63187616748886, 
7.65717987566166, 7.74016035808493, 7.79327615657051, 7.61036111666667, 
6.93055923840536, 6.98454642755329, 7.08191653567892, 7.149035614177, 
7.15703819845921, 7.15266967093479, 7.19286008867969, 7.17396359743953, 
7.19774908312196, 7.27575073659984, 7.32567958717628, 7.15373944966667, 
7.22547665280559, 7.2817611691513, 7.38327468613334, 7.45324989563134, 
7.46159301541492, 7.45703859310223, 7.49893924138947, 7.47923864413908, 
7.50403627814842, 7.58535715092323, 7.6374106334391, 7.45815389433333
), measure = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
), NewDate = structure(c(1354320000, 1356998400, 1359676800, 
1362096000, 1364774400, 1367366400, 1370044800, 1372636800, 1375315200, 
1377993600, 1380585600, 1383264000, 1385856000, 1388534400, 1391212800, 
1393632000, 1396310400, 1398902400, 1401580800, 1404172800, 1406851200, 
1409529600, 1412121600, 1414800000, 1417392000, 1420070400, 1422748800, 
1425168000, 1427846400, 1430438400, 1433116800, 1435708800, 1438387200, 
1441065600, 1443657600, 1446336000, 1448928000, 1451606400, 1438387200, 
1441065600, 1443657600, 1446336000, 1448928000, 1451606400, 1454284800, 
1456790400, 1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 
1472688000, 1475280000, 1477958400, 1480550400, 1451606400, 1454284800, 
1456790400, 1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 
1472688000, 1475280000, 1477958400, 1480550400, 1451606400, 1454284800, 
1456790400, 1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 
1472688000, 1475280000, 1477958400, 1480550400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), .Names = c("variable", "value", "measure", 
"NewDate"), row.names = c(5052L, 5053L, 5054L, 5055L, 5056L, 
5057L, 5058L, 5059L, 5060L, 5061L, 5062L, 5063L, 5064L, 5065L, 
5066L, 5067L, 5068L, 5069L, 5070L, 5071L, 5072L, 5073L, 5074L, 
5075L, 5076L, 5077L, 5078L, 5079L, 5080L, 5081L, 5082L, 5083L, 
5084L, 5085L, 5086L, 5087L, 5088L, 5089L, 5336L, 5337L, 5338L, 
5339L, 5340L, 5341L, 5342L, 5343L, 5344L, 5345L, 5346L, 5347L, 
5348L, 5349L, 5350L, 5351L, 5352L, 5593L, 5594L, 5595L, 5596L, 
5597L, 5598L, 5599L, 5600L, 5601L, 5602L, 5603L, 5604L, 5845L, 
5846L, 5847L, 5848L, 5849L, 5850L, 5851L, 5852L, 5853L, 5854L, 
5855L, 5856L), class = "data.frame")

I can create a geom_line() chart as seen here using:

ggplot(ex_data, aes(x=NewDate, y=value, colour=variable)) + geom_line()

basic geom_line() chart

However, I'd like to be able to fill in the area under the Proj (blue) line until it meets the Thresh (purple) line, the area under the Thresh line until it meets the Goal (green) line and everything under the Goal line to y = 0.

When I try a basic area chart for all lines, the chart seems to distort the y-values:

ggplot(ex_data, aes(x=NewDate, y=value)) + geom_area(aes(fill=variable)) + geom_line()

basic area chart

I tried to follow this example here: Filling in the area under a line graph in ggplot2: geom_area()

The end result would be very similar to a forecast chart where the first line has no fill but the last 3 lines have fills between them like this one: enter image description here

Community
  • 1
  • 1
tcash21
  • 4,880
  • 4
  • 32
  • 39
  • The series don't line up: how do you envision "fill[ing] in the area under the Proj (blue) line until it meets the Thresh (purple) line" for x < 2016? You might want to clarify what you are trying to express with the final plot – arvi1000 Jul 05 '16 at 23:39
  • You're right, I believe I'd want to fill only where the dates overlap for those last 3 groups. – tcash21 Jul 06 '16 at 14:26
  • 1
    Best bet might be to use geom_polygon to trace the contours of what you want to fill. That will mean adding points for the 'lower' boundary of the fill zone to each series. Doesn't sound like this really adds any information to the plot though. – arvi1000 Jul 06 '16 at 16:26

1 Answers1

2

By default, geom_area stacks different fills, leading to the high y-values. You can turn this off with the position argument. This is probably closer to what you are after.

ggplot(ex_data, aes(x = NewDate, y = value, ymax = value, colour = variable, fill = variable)) + 
  geom_area(position = "identity") + 
  geom_line()
Richard Telford
  • 9,558
  • 6
  • 38
  • 51
  • Thanks, this is a lot closer, now I just need to figure out how to fill area only under certain lines and limit the y-ranges for each group. – tcash21 Jul 06 '16 at 14:26