cmirian
cmirian

Reputation: 2253

How to "fill" area below geom_line predicted from model in ggplot2 / R?

Please, find My Data below as sample of my dataset nd.

head(nd)
           y lnd
1: 0.3479943   0
2: 0.3740154   1
3: 0.3996300   2
4: 0.4217426   3
5: 0.4403666   4
6: 0.4572609   5

I have estimated a risk (denoted nd$y), which is the 5-year risk of death given the value corresponding to the continuous variable nd$lnd, which ranges from nd$lnd==0,1,2,...,100.

The predicted model look like this:

enter image description here

The ggplot was produced with the following script:

ggplot(as.data.frame(nd), aes(x = lnd)) +
  geom_line(aes(y = y, col = lnd),size=1) + theme(legend.position="none") +
  ggtitle("Lymph node density", subtitle = "Risk groups") + 
  scale_x_continuous(name="Evidence of metastatic burden per lymph node yield", breaks=seq(0,100,by=10), limits=c(0,100), label=c("0%\n LND 0%","10%","20%", "30%", "40%","50%","60%","70%","80%","90%","100%")) +
  scale_y_continuous(name="5-year risk of death", breaks = seq(0.3,1,by=.1),label=c("30%","40%","50%","60%","70%","80%","90%","100%")) + coord_cartesian(ylim=c(0.15,1)) 

I would like to add a "filled" area below the predicted geom_line()

I have tried to demonstrate in Photoshop what I would like:

enter image description here

It is the area between nd$lnd==0 and nd$lnd==10 on the x-axis and below the corresponding nd$y-values for the specific nd$lnd==0 - 10

My Data

 nd <- structure(list(y = c(0.347994323433218, 0.374015418531899, 0.399629966119251, 
                        0.421742599051725, 0.44036657229005, 0.457260948328608, 0.474417648309559, 
                        0.493640865541709, 0.51488973428659, 0.537423493454801, 0.560430703662936, 
                        0.583045245963954, 0.604403317363394, 0.624118977779026, 0.642197094829998, 
                        0.658675856783435, 0.673612299496595, 0.687077654264746, 0.699153267191595, 
                        0.709927163750076, 0.719491270455802, 0.727939260908065, 0.735364964416906, 
                        0.741861259442995, 0.747519368138831, 0.752428469501541, 0.756675554521809, 
                        0.760345455237322, 0.763520989282281, 0.766283171326885, 0.768711452044572, 
                        0.770883953521034, 0.772877677101532, 0.774768665456197, 0.776627848464512, 
                        0.778481560816502, 0.780330067279974, 0.782173296838651, 0.784011178693879, 
                        0.785843642275446, 0.787670617252314, 0.78949203354347, 0.791307821328959, 
                        0.793117911060613, 0.794922233473425, 0.796720719596049, 0.798513300762498, 
                        0.800299908622762, 0.802080475154149, 0.803854932672504, 0.805623213843368, 
                        0.807385251693197, 0.809140979620611, 0.810890331407796, 0.812633241231531, 
                        0.814369643674837, 0.816099473738181, 0.81782266685046, 0.819539158880875, 
                        0.821248886149957, 0.822951785441022, 0.824647794011433, 0.826336849604117, 
                        0.828018890458497, 0.829693855322157, 0.831361683462205, 0.833022314676256, 
                        0.834675689303694, 0.836321748237183, 0.837960432933643, 0.839591685425587, 
                        0.841215448331989, 0.84283166486968, 0.844440278864124, 0.846041234760886, 
                        0.847634477636162, 0.849219953207282, 0.850797607844881, 0.852367388582385, 
                        0.853929243127316, 0.855483119871763, 0.857028967903429, 0.858566737015336, 
                        0.860096377717062, 0.861617841244557, 0.863131079570552, 0.864636045414574, 
                        0.866132692253169, 0.867620974331152, 0.869100846667809, 0.870572265071487, 
                        0.872035186145084, 0.873489567298754, 0.874935366756863, 0.87637254356889, 
                        0.877801057617996, 0.879220869629812, 0.8806319411832, 0.882034234714961, 
                        0.883427713533749, 0.884812341823933), lnd = c(0, 1, 2, 3, 4, 
                                                                       5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 
                                                                       22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 
                                                                       38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 
                                                                       54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 
                                                                       70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 
                                                                       86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)), row.names = c(NA, 
                                                                                                                                                    -101L), class = c("data.table", "data.frame"))

Upvotes: 1

Views: 118

Answers (1)

mgiormenti
mgiormenti

Reputation: 833

You could try using geom_area for the subset of the data that you want to plot:

+ geom_area(aes(y = y), data = function(x) subset(x, lnd >= 0 & lnd <= 10))

The only issue is that it plots the area up to the Y = 0 axis, not just to the 30% that you colored in photoshop.

In case it helps, here I talk briefly about plotting a layer with only a subset of the default data.

Upvotes: 2

Related Questions