几何色带,曲线下方的阴影区域高于特定阈值?

问题描述

我似乎无法对曲线下方的区域进行阴影处理,geom_ribbon 似乎已经对正确的区域进行了阴影处理,但是我想将阴影区域放置在曲线下方,以便当 y 值是大于 120 时,曲线下方区域显示为黄色,当 y 值大于 130 时,曲线下方区域显示为橙色,当 y 值大于 140 时,曲线下方区域显示为红色。我怀疑它与 geom_line 参数有关。我花了几天时间思考解决方案,并尝试了许多其他解决方案,但无济于事。这是我正在使用的数据框的片段

structure(list(meal_type = structure(c(1L,1L,2L,4L,4L),.Label = c("WO","HF","HP","lgi"),class = "factor"),subject = c(35,35,35),timestamp = structure(c(1617754260,1617755160,1617756060,1617756960,1617757860,1617758760,1617759660,1617760560,1617761460,1617762360,1617763260,1617764160,1617765060,1617765960,1617766860,1617767760,1617768660,1617769560,1617770460,1617771360,1617772260,1617773160,1617774060,1617774960,1617775860,1617776760,1617777660,1617778560,1617779460,1617780360,1617781260,1617782160,1617783060,1617783960,1617784860,1617785760,1617786660,1617787560,1617788460,1617789360,1617790260,1617791160,1617792060,1617792960,1617793860,1617794760,1617795660,1617796560,1617797460,1617798360,1617799260,1617800160,1617801060,1617801960,1617802860,1617803760,1617804660,1617805560,1617806460,1617807360,1617808260,1617809160,1617810060,1617810960,1617811860,1617812760,1617813660,1617814560,1617815460,1617816360,1617817260,1617818160,1617819060,1617819960,1617820860,1617821760,1617822660,1617823560,1617824460,1617825360,1617826260,1617827160,1617828060,1617828960,1617829860,1617830760,1617831660,1617832560,1617833460,1617834360,1617835260,1617836160,1617837060,1617837960,1617838860,1617839760,1617840660,1617841560,1617842460,1617843360,1617844260,1617845160,1617846060,1617846960,1617847860,1617848760,1617849660,1617850560,1617851460,1617852360,1617853260,1617854160,1617855060,1617855960,1617856860,1617857760,1617858660,1617859560,1617860460,1617861360,1617862260,1617863160,1617864060,1617864960,1617865860,1617866760,1617867660,1617868560,1617869460,1617870360,1617871260,1617872160,1617873060,1617873960,1617874860,1617875760,1617876660,1617877560,1617878460,1617879360,1617880260,1617881160,1617882060,1617882960,1617883860,1617884760,1617885660,1617886560,1617887460,1617888360,1617889260,1617890160,1617891060,1617891960,1617892860,1617893760,1617894660,1617895560,1617896460,1617897360,1617898260,1617899160,1617900060,1617900960,1617901860,1617902760,1617903660,1617904560,1617905460,1617906360,1617907260,1617908160,1617909060,1617909960,1617910860,1617911760,1617912660,1617913560,1617914460,1617915360,1617916260,1617917160,1617918060,1617918960,1617919860,1617920760,1617921660,1617922560,1617923460,1617924360,1617925260,1617926160,1617927060,1617927960,1617928860,1617929760,1617930660,1617931560,1617932460,1617933360,1617934260,1617935160,1617936060,1617936960,1617937860,1617938760,1617939660,1617940560,1617941460,1617942360,1617943260,1617944160,1617945060,1617945960,1617946860,1617947760,1617948660,1617949560,1617950460,1617951360,1617952260,1617953160,1617954060,1617954960,1617955860,1617956760,1617957660,1617958560,1617959460,1617960360,1617961260,1617962160,1617963060,1617963960,1617964860,1617965760,1617966660,1617967560,1617968460,1617969360,1617970260,1617971160,1617972060,1617972960,1617973860,1617974760,1617975660,1617976560,1617977460,1617978360,1617979260,1617980160,1617981060,1617981960,1617982860,1617983760,1617984660,1617985560,1617986460,1617987360,1617988260,1617989160,1617990060,1617990960,1617991860,1617992760,1617993660,1617994560,1617995460,1617996360,1617997260,1617998160,1617999060,1617999960,1618000860,1618001760,1618002660,1618003560,1618004460,1618005360,1618006260,1618007160,1618008060,1618008960,1618009860,1618010760,1618011660,1618012560,1618013460,1618014360,1618015260,1618016160,1618017060,1618017960,1618018860,1618019760,1618020660,1618021560,1618022460,1618023360,1618024260,1618025160,1618026060,1618026960,1618027860,1618028760,1618029660,1618030560,1618031460,1618032360,1618033260,1618034160,1618035060,1618035960,1618036860,1618037760,1618038660,1618039560,1618040460,1618041360,1618042260,1618043160,1618044060,1618044960,1618045860,1618046760,1618047660,1618048560,1618049460,1618050360,1618051260,1618052160,1618053060,1618053960,1618054860,1618055760,1618056660,1618057560,1618058460,1618059360,1618060260,1618061160,1618062060,1618062960,1618063860,1618064760,1618065660,1618066560,1618067460,1618068360,1618069260,1618070160,1618071060,1618071960,1618072860,1618073760,1618074660,1618075560,1618076460,1618077360,1618078260,1618079160,1618080060,1618080960,1618081860,1618082760,1618083660,1618084560,1618085460,1618086360,1618087260,1618088160,1618089060,1618089960,1618090860,1618091760,1618092660,1618093560,1618094460,1618095360,1618096260,1618097160,1618098060),tzone = "UTC",class = c("POSIXct","POSIXt")),sensorglucose = c(76,77,82,88,90,89,86,83,80,78,81,79,76,73,71,70,75,69,64,66,67,63,68,92,104,93,97,91,72,95,111,108,85,94,87,103,114,102,84,98,132,130,116,107,99,74,65,100,101,106,96,61,62,117,119,118,110,105,113,109,144,137,115,91),day_extract = c("07","07","08","09","10","10"),change = c(-5,-4,1,7,9,8,5,2,-1,-3,-2,-5,-8,-10,-11,-6,-12,-17,-15,-14,-18,-13,11,23,12,16,10,-9,14,30,27,4,13,6,22,33,21,3,17,51,49,26,18,15,29,25,28,19,20,32,-19,-7,-16,24,37,39,38,34,31,-20,-24,-23,-22,-21,59,52,6),diff = c(1,0
),iauc = c(0,225,2.30769230769231,45,52.5,16.875,3.97058823529412,9.64285714285714,172.8,307.5,98.5227272727273,0),class = c("normal","normal","hyper-2","hyper-3","normal")),row.names = c(NA,-383L),class = c("data.table","data.frame"),.internal.selfref = <pointer: 0x000002244b801ef0>)

以及我迄今为止所尝试的

figure1 = ggplot(variableh,aes(x = timestamp,y = sensorglucose)) + 
  geom_line(aes(color = meal_type,group = 1)) + #geom_ribbon(aes(timestamp,ymin = 120,ymax = 140),fill = "pink",alpha = 0.5) + 
  geom_ribbon(data = variableh[variableh$class == "hyper-1",],aes(timestamp,y=sensorglucose,ymax = sensorglucose),fill = "yellow",alpha = 0.5) +
  geom_ribbon(data = variableh[variableh$class == "hyper-2",ymin = 130,fill = "orange",alpha = 0.5) +
  geom_ribbon(data = variableh[variableh$class == "hyper-3",ymin = 140,fill = "red",alpha = 0.5) +
  ggtitle(paste("Subject ",i," 24h Interstitial glucose Levels")) + xlab('Date') + ylab('Interstitial glucose (mg/dL)') + scale_color_manual(values = c("#EF476F","#F6A21E","#06D6A0","#118AB2"))
figure1

生成的图像是这样的:

The areas are not supposed to exceed the curve and are under the curve

但这是所需的输出

enter image description here

解决方法

这是一个使用 ggplot

的可能解决方案

它涉及为每种颜色创建一个新列,其中 sensorglucose 变量仅在值高于阈值时才存在,否则为 NA。 通过这种方式,可以为每种颜色使用 geom_ribbon

library(tidyverse)

variableh <- variableh %>% 
  mutate(
  glucose_yellow = ifelse(sensorglucose > 120,sensorglucose,NA),glucose_orange = ifelse(sensorglucose > 130,glucose_red = ifelse(sensorglucose > 140,NA)
)

ggplot(variableh,aes(x = timestamp,y = sensorglucose)) +
  geom_line() +
  geom_ribbon(aes(ymax = glucose_yellow,ymin=120),fill="yellow") +
  geom_ribbon(aes(ymax = glucose_orange,ymin=130),fill="orange") +
  geom_ribbon(aes(ymax = glucose_red,ymin=140),fill="red")

这是结果

但是,根据您的数据和阈值,您几乎看不到彩色区域。 这里我有相同的代码,我更改了级别以更好地可视化绘图。

variableh_test <- variableh %>% 
  mutate(
    glucose_yellow = ifelse(sensorglucose > 70,glucose_orange = ifelse(sensorglucose > 90,glucose_red = ifelse(sensorglucose > 100,NA)
  )

ggplot(variableh_test,y = sensorglucose)) +
  geom_ribbon(aes(ymax = glucose_yellow,ymin=70),ymin=90),ymin=100),fill="red")+
  geom_line(size=.7) #increase line size to see it better 

编辑:

可以通过将标签放在 aes 中然后使用 scale_fill_manual 覆盖颜色来将图例添加到不同的颜色。 我不知道有什么方法可以将图例重新排序以在顶部显示红色

ggplot(variableh_test,ymin=70,fill="yellow label" )) +
  geom_ribbon(aes(ymax = glucose_orange,ymin=90,fill="orange label")) +
  geom_ribbon(aes(ymax = glucose_red,ymin=100,fill="red label"))+
  geom_line(size=.7) + #increase line size to see it better
  scale_fill_manual(values=c("yellow label" = "yellow","orange label" = "orange","red label" = "red")) +
  labs(fill="glucose level")

enter image description here