使用ggplot2填充某些行之下和之间的区域

时间:2016-07-05 23:05:02

标签: r ggplot2

以下是示例数据:

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")

我可以创建一个geom_line()图表,如下所示:

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

basic geom_line() chart

然而,我希望能够填充Proj(蓝色)线下的区域,直到它遇到Thresh(紫色)线,Thresh线下面的区域,直到它达到目标(绿色)线并且目标线下的所有内容都为y = 0。

当我尝试所有线条的基本面积图时,图表似乎会扭曲y值:

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

basic area chart

我尝试在此处遵循此示例:Filling in the area under a line graph in ggplot2: geom_area()

最终结果非常类似于预测图表,其中第一行没有填充但最后3行之间有填充,如下所示: enter image description here

1 个答案:

答案 0 :(得分:2)

默认情况下,geom_area会堆叠不同的填充,从而导致高y值。您可以使用位置argument关闭此功能。这可能更接近你所追求的目标。

ggplot(ex_data, aes(x = NewDate, y = value, ymax = value, colour = variable, fill = variable)) + 
  geom_area(position = "identity") + 
  geom_line()