0

I fail to plot correcly my stacked density chart

here is my data structure

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   8517 obs. of  3 variables:
 $ time  : num  -500 -499 -498 -497 -496 -495 -494 -493 -492 -491 ...
 $ series: Factor w/ 17 levels "V1","V2","V3",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value : num  0.0788 0.0625 0.0455 0.0323 0.0426 ...

and here is my code

ggplot(data=data,aes(x=time, group=data$series, fill=data$series)) + 
    geom_density(adjust=1.5, position="fill")

but it plots me horizontal lines

enter image description here

Instead I would like it plots me something like that

enter image description here

If anyone has any idea? I would appreciate. Thanks

here is my data structure

dput(head(data))
    structure(list(time = c(-500, -499, -498, -497, -496, -495), 
        series = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("V1", 
        "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", 
        "V12", "V13", "V14", "V15", "V16", "V17"), class = "factor"), 
        value = c(0.0787576635451054, 0.0625, 0.0455255463892602, 
        0.0322986577181208, 0.0426306596802365, 0.0369765758489718
        )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
    ))
nico dm
  • 19
  • 3
  • How can I plot y value? ,this command aes(x=time,y = value, group=series, fill=series) returns me Warning message: Removed 8192 rows containing missing values (position_stack). – nico dm Jul 26 '18 at 14:46
  • Copy exactly the output from `dput()` and update your question above, not here in the comments, thanks. – RLave Jul 26 '18 at 15:01
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example see here for any questions, it's a guide for a reproducible example. – RLave Jul 26 '18 at 15:01
  • 1
    ok I understand :) – nico dm Jul 26 '18 at 15:11

2 Answers2

0

Does this work for you?

ggplot(data, aes(x=time, fill = series)) + 
  geom_density(adjust=1.5, position="fill")

My bad, initially I got the wrong idea, you were almost there.

Just remove data$.

RLave
  • 8,144
  • 3
  • 21
  • 37
0

For the sake of illustrating it with multiple factors, I changed it a tad.

df <- structure(list(time = c(-500, -499, -498, -497, -496, -495), 
           series = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("V1", 
                                                                    "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", 
                                                                    "V12", "V13", "V14", "V15", "V16", "V17"), class = "factor"), 
           value = c(0.0787576635451054, 0.0625, 0.0455255463892602, 
                     0.0322986577181208, 0.0426306596802365, 0.0369765758489718
           )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
           ))


ggplot(df, aes(x = time)) +
  geom_density(stat = "density", aes(fill = series), position = "stack") # or fill if you want proportions  

Stack Fill

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29
  • this is it but with my datas it stil produce me horizontal lines – nico dm Jul 26 '18 at 15:26
  • This is basically the same code of mine. I get the same result. – RLave Jul 26 '18 at 15:27
  • I'm not sure why you're getting lines with the same data you provided. If the data are all proportionally equal, you'd see the first image in your question. In your `str(data)` it lists 8517 observations. Are you running this on your larger set? – Anonymous coward Jul 26 '18 at 17:37