stacked density plot with gg plot in R

The name of the picture


stacked density plot with gg plot in R



I fail to plot correcly my stacked density chart



here is my data structure


data


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





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
32 mins ago





Copy exactly the output from dput() and update your question above, not here in the comments, thanks.
– RLave
17 mins ago


dput()





stackoverflow.com/questions/5963269/… see here for any questions, it's a guide for a reproducible example.
– RLave
17 mins ago





ok I understand :)
– nico dm
7 mins ago




2 Answers
2



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$.


data$



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Regex - How to capture all iterations of a repeating pattern?