将值分组到存储箱中,然后使用plotly(R、Dplyr)打印
我有一个数据集df,它告诉我们X是频率,类别是“bin” 其中X值属于。所以X告诉我们这个类别出现了多少次。这是实际数据集的一个小样本 图1 我从另一个类似图3的数据集中使用此代码创建了上述输出将值分组到存储箱中,然后使用plotly(R、Dplyr)打印,r,dplyr,plotly,histogram,R,Dplyr,Plotly,Histogram,我有一个数据集df,它告诉我们X是频率,类别是“bin” 其中X值属于。所以X告诉我们这个类别出现了多少次。这是实际数据集的一个小样本 图1 我从另一个类似图3的数据集中使用此代码创建了上述输出 Fig. 2 df1 <- aggregate(df$gr, by=list(Category=data$Duration), FUN=length) Fig. 3 gr Duration Outdata1 1
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
因此,根据您所做的工作,我没有筛选任何类型的数据。但使用tidyverse软件包,我会做以下几点:
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")
好的,我会试试这个,这基本上是使用ggplot创建一个直方图,其中包含以10为增量分组的箱子?非常感谢。是的,这就是“切割”功能,它起作用了!有没有一种方法来设计颜色?col=蓝色??geom_barstat=标识,填充=蓝色
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]
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")
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")