我有一些数据,一些事件发生在一天中的某些时间,在特定的条件下。data_frame看起来有点像这样:
> tibble(event_id = 1:1000, hour = rep_len(0:23, 1000), conditions = rep_len(c("Non", "Oui"), 1000))
# A tibble: 1,000 × 3
event_id hour conditions
<int> <int> <chr>
1 1 0 Non
2 2 1 Oui
3 3 2 Non
4 4 3 Oui
5 5 4 Non
6 6 5 Oui
7 7 6 Non
8 8 7 Oui
9 9 8 Non
10 10 9 Oui 不知何故,我设法以这种方式使用geom_bar来表示它:
mydataframe %>%
group_by(hour, conditions) %>%
count() %>%
ggplot() +
geom_bar(aes(x = hour, y = n, fill = conditions), stat = "identity", position = "dodge")根据我的实际数据,我得到的数字如下:

但我想得到一些东西,像2回避平滑线或geom_density,我似乎无法得到。你有什么主意要帮我吗?
谢谢
发布于 2022-09-10 20:22:20
library(tidyverse)
set.seed(42)
mydataframe <- tibble(event_id = 1:1000, hour = rep_len(0:23, 1000), conditions = sample(c("Non", "Oui"), 1000, replace = TRUE))
mydataframe %>%
count(hour, conditions) %>%
ggplot() +
geom_smooth(aes(hour, n, color = conditions), se = FALSE, span = 0.3)

或者,如果您想避开它们,您可以这样做,并调整系列之间的宽度:
mydataframe %>%
count(hour, conditions) %>%
ggplot() +
geom_smooth(aes(hour, n, color = conditions), se = FALSE, span = 0.3,
position = position_dodge(width = 1))

https://stackoverflow.com/questions/73673360
复制相似问题