0

This is the dput output.

structure(list(data = structure(list(stations = c("s1", "s2", 
"s3", "s4", "s5", "s1", "s2", "s3", "s4", "s5", "s1", "s2", "s3", 
"s4", "s5", "s1", "s2", "s3", "s4", "s5"), Mean = c(792.0666667, 
830.0666667, 542.4666667, 311.3333333, NA, 535.3222222, 576.9855556, 
510.0756667, 433.9747778, 347.35, 726.9027778, 798.3153333, 
706.2138889, 593.0828889, 474.9132222, 991.5945841, 1044.328044,
693.3808187, 403.8107808, NA), stdev = c(189.6764965, 123.9226103, 
115.742876, 70.03437251, NA, 300.4539788, 306.3421887, 274.6515927, 
237.7002122, 191.230147, 197.9449891, 248.458886, 203.0634826, 
156.500875, 124.8746002, 265.4692578, 190.0975192, 185.9667516, 
119.4178709, NA), Lower = c(602.3901701, 706.1440564, 426.7237907, 
241.2989608, NA, 234.8682435, 270.6433669, 235.424074, 196.2745655, 
156.119853, 528.9577886, 549.8564473, 503.1504063, 436.5820139, 
350.038622, 726.1253263, 854.2305244, 507.4140671, 284.3929099, NA), 
Upper = c(981.7431632, 953.9892769, 658.2095426, 381.3677058, NA, 
835.776201, 883.3277442, 784.7272594, 671.67499, 538.580147, 
924.8477669, 1046.774219, 909.2773715, 749.5837639, 599.7878224, 
1257.063842, 1234.425563, 879.3475703, 523.2286518, NA), Group = 
c("Observation", "Observation", "Observation", "Observation", 
"Observation", "Historical", "Historical", "Historical", "Historical", 
"Historical", "Future", "Future", "Future", "Future", "Future", 
"Downscaled", "Downscaled", "Downscaled", "Downscaled", "Downscaled")), 
class = "data.frame", row.names = c(NA, -20L)), layers = 
list(<environment>, <environment>), scales = <environment>, 
mapping = structure(list(x = ~Mean, Group = ~Group), class = 
"uneval"), theme = list(), coordinates = <environment>, facet = 
<environment>, plot_env = <environment>, labels = list(title = 
"Probability density functions (PDF) for in comparison with 
observations", y = "Probability density function", x = "March maximum 
temperature", Group = "Group", colour = "factor(Group)", linetype = 
"factor(Group)", fill = structure("fill", fallback = TRUE), weight = 
structure("weight", fallback = TRUE))), class = c("gg", 
"ggplot"))

I have produced density plot using ggplot2. The density plot is the mean. I want to shade each density line by lower and upper bounds just like confidence intervals. Please can anyone help how to achieve this?

The code I used to generate density in ggplot

ggplot(pdf1, aes(x=Mean, Group=Group)) + 
geom_density(aes(colour=factor(Group), linetype=factor(Group)),
show.legend = FALSE) + 
stat_density(aes(x=Mean, colour=factor(Group), 
linetype=factor(Group)),
           geom="line",position="identity", linewidth=1) +
xlab("March maximum temperature") +
ylab("Probability density function") + 
ggtitle("Probability density functions (PDF) for in comparison with 
observations") +
scale_linetype_manual(values=c('solid', 'solid','solid','dotted'),
                    labels=c("Observation", "Historical", "Future", 
"Downscaled")) +
scale_colour_manual(values = c("black", "green", "blue", "red"),
                  labels=c("Observation", "Historical", "Future", 
"Downscaled"))

This is the output from the above code Since my reputation in stackoverflow is only 6 and I need at least 10 to show the image, I am not able to show the image here but can see through this link

I searched in the net and could not find the solution.

Thanks a lot for your help!

This is a kind of output I am looking for (https://rpubs.com/Grady/875225). But as you can see, it has both x and y variables but I only have one variable (Mean in my case) in the x-axis while it is probability density in the y-axis in the above ggplot. So, I want to use the lower and upper values to envelope each line with shades with each respective color of my density plothttps://rpubs.com/Grady/875225

sdorji
  • 39
  • 4
  • 3
    Please don't show a picture of your code or data but [provide a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (i.e. paste the output of `dput` of your data), then it's easier to help you, thanks! – starja Jan 20 '23 at 15:48
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 20 '23 at 19:37
  • @starja I have responded to your comments above. Thanks for your help! – sdorji Jan 22 '23 at 23:10
  • 1
    What are you exactly looking for in the output, OP? It's not clear what you mean by "shade density line by lower and upper bounds". – chemdork123 Jan 23 '23 at 14:26
  • @chemdork123 Thank you for your comment. I am actually trying to do geom_ribbon for my lower and upper values but since I have density on my y-axis, I don't know how to add ribbon. geom_ribbon requires x-values and y-values but I only have x-values. I am also editing my question and adding a kind of output I am looking for. Thanks – sdorji Jan 24 '23 at 00:52
  • 1
    If you have a density plot, the value in the y axis can be "accessed" within `aes()` by `..density..` (value of the density function) or `..count..` (for the base number). So, it would look like `geom_ribbon( aes(x=your_x_value, y=..density..), ...)` or something similar to that. – chemdork123 Jan 30 '23 at 16:03
  • Thank you chemdork123! Sorry for my late response. – sdorji Jun 13 '23 at 10:04

0 Answers0