I discovered this amazing R package ggridges
and would like to use it for a graph, however, I am failing at executing the code, as I am probably wrongly putting my variables. I was hoping someone could guide me in obtaining the result I want.
Please see my table below in R
Data frame
g<-data.frame(structure(list(session = c("s1", "s1", "s1", "s1", "s1", "s1",
"s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1", "s1",
"s1", "s1", "s1", "s1", "s1", "s2", "s2", "s2", "s2", "s2", "s2",
"s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2", "s2",
"s2", "s2", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3",
"s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3", "s3",
"s3", "s4", "s4", "s4", "s4", "s4", "s4", "s4", "s4", "s4", "s4",
"s4", "s4", "s4", "s4", "s4", "s4", "s4", "s5", "s5", "s5", "s6",
"s6", "s6", "s6", "s7", "s7", "s7", "s7", "s7"), interval = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L,
10L, 10L, 11L, 11L, 12L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L,
5L, 6L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 12L, 1L, 2L, 2L, 3L, 3L,
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L,
12L, 1L, 2L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L,
10L, 11L, 12L, 9L, 11L, 12L, 1L, 7L, 11L, 12L, 3L, 4L, 5L, 6L,
7L), .Label = c("120", "240", "360", "480", "600", "720", "840",
"960", "1080", "1200", "1320", "1440"), class = "factor"), person = c("person2",
"person1", "person2", "person1", "person2", "person1", "person1", "person2",
"person1", "person2", "person1", "person2", "person1", "person2", "person1",
"person2", "person1", "person2", "person1", "person2", "person1", "person1",
"person2", "person1", "person2", "person1", "person2", "person1", "person2",
"person1", "person2", "person1", "person2", "person1", "person1", "person1",
"person1", "person1", "person1", "person2", "person1", "person1", "person2",
"person1", "person2", "person1", "person1", "person2", "person1", "person2",
"person1", "person2", "person1", "person2", "person1", "person2", "person1",
"person2", "person1", "person2", "person1", "person1", "person2", "person2",
"person2", "person2", "person1", "person2", "person1", "person2", "person1",
"person2", "person1", "person2", "person1", "person1", "person2", "person2",
"person2", "person1", "person2", "person2", "person1", "person1", "person1",
"person1", "person1", "person1", "person1", "person1", "person1"), counts = c(12L,
51L, 15L, 43L, 4L, 54L, 42L, 12L, 51L, 9L, 73L, 3L, 38L, 12L,
58L, 13L, 58L, 9L, 62L, 13L, 40L, 21L, 2L, 8L, 31L, 15L, 27L,
29L, 17L, 13L, 22L, 4L, 9L, 29L, 25L, 4L, 26L, 10L, 4L, 1L, 7L,
18L, 3L, 19L, 2L, 36L, 38L, 12L, 29L, 8L, 15L, 10L, 20L, 14L,
11L, 9L, 23L, 13L, 20L, 8L, 6L, 10L, 1L, 6L, 15L, 6L, 5L, 4L,
17L, 7L, 4L, 6L, 2L, 6L, 14L, 4L, 2L, 12L, 1L, 3L, 5L, 4L, 19L,
7L, 6L, 12L, 4L, 6L, 11L, 9L, 8L)), row.names = c(NA, -91L), class = c("tbl_df",
"tbl", "data.frame")))
Columns
- session: is the grouping context of my data
- interval: are regular pre-defined time-slots/stamps (here: every 120 seconds) within each session
- person: there are two people from whom the data was taken
- counts: is the number of observations made within each interval for each person in each session
R Code
I firstly experiment with a code for ggridges
:
ggplot(
g,
aes(x = `counts`, y = `session`, fill = stat(x))
) +
geom_density_ridges_gradient(scale = 3, size = 0.3, rel_min_height = 0.01) +
scale_fill_viridis_c(name = "counts", option = "C")
Output
This output looks very promising. Adding the following line + facet_grid(.~person)
would give me a graph for person 1 and 2, which is what I am looking for. It is also great to see that the colors represent the frequency of counts (darker color stand for fewer and lighter color for more counts)
Problem description
My main problem with the code is that no matter how I twist it and tweak it (adding height = counts, fill= counts
), I don't see the graph because some parts of the code work and other's do not. In particular,
- session: should be displayed on the y-axis (works!)
- interval: should be displayed on the x-axis (works!)
- counts: should be displayed as the height of the density curve and have a different gradient color depending on the frequency of counts (doesn't work!)
- person: I want to have separate graphs for person 1 and 2 (works if I add
+ facet_grid(.~person)
)
Desired graph
Although made with a pen-and-paper method and being not very elegant, this is the desired graph I am aiming at getting. The peaks on the density curves (in circles) correspond to counts
pointed out for each session and each person in my data frame.
Maybe my approach is wrong, and the package is not the one I am looking for, but if not I would appreciate if someone with a higher expertise in R could advise me on this issue.