Reputation: 4398
I have a dataset, df, which is telling us that X is the frequency and Category is the 'bin in which the X value belongs. So X is telling us how many times the Category occurs. (This is a small sample of the actual dataset)
Fig.1
Category X
100 5
101 10
110 20
120 5
125 2
150 1
I created the above output using this code from another dataset that looked like Fig.3
Fig. 2 df1 <- aggregate(df$gr, by=list(Category=data$Duration), FUN=length)
Fig. 3 gr Duration
Outdata1 100
Outdata2 101
Outdata3 110
Outdata4 120
Outdata5 125
Outdata6 150
Here is a sample of my plotly graph:
p <- plot_ly(data = df,
x = ~Category,
y = ~x,
name = "name",
type = "bar",
orientation = 'v'
)%>%
layout(
title = "title",
xaxis = list(title = "Time in Seconds" , categoryorder = "ascending",tickangle = -45 ),
yaxis = list(title = "example",
barmode = "group"
))
[![enter image description here][1]][1]
However, instead of the Category displaying as individual values, I want to group them in 'bins' like a histogram, like this:
So that the Category reveals bins in increments of 10, so the Category would be like: 100 110 120 130 140 150 , vs. 100 101 110 120 125 150
Here is the dput for Fig. 1
structure(list(Category = structure(c(0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 42, 43, 44, 47,
48, 49, 50, 51, 52, 53, 55, 56, 57, 58, 60, 63, 65, 66, 67, 68,
69, 70, 71, 72, 74, 77, 79, 80, 82, 84, 87, 89, 90, 91, 96, 97,
98, 103, 110, 114, 116, 124, 125, 126, 133, 134, 143, 149, 152,
154, 155, 157, 158, 161, 163, 164, 173, 177, 179, 183, 184, 185,
189, 190, 193, 196, 198, 201, 207, 211, 214, 217, 227, 229, 234,
235, 248, 265, 270, 285, 293, 307), class = "difftime", units = "secs"),
x = c(1L, 1L, 1L, 5L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 7L,
2L, 2L, 4L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 4L, 3L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 4L, 2L, 2L, 4L,
1L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L
)), row.names = c(NA, -118L), class = "data.frame")
Upvotes: 0
Views: 546
Reputation: 3994
So based on what you are doing, I did not filter for any kind of data. But using tidyverse
package, here is what I would do:
dfs %>%
mutate(newvar = as.numeric(gsub(" secs", "", Category)),
new_cat = cut(newvar, breaks = seq(0,round(max(newvar), -1), by = 10), include.lowest = T)) %>%
group_by(new_cat) %>%
summarise(Counts = sum(x)) %>%
ungroup() %>%
ggplot(aes(x = new_cat, y = Counts)) +
geom_bar(stat = "identity")
Upvotes: 1