gpt4 book ai didi

r - 如何将geom_bar、geom_point、geom_line合并为一张图?

转载 作者:行者123 更新时间:2023-12-04 17:33:18 24 4
gpt4 key购买 nike

我正在尝试通过三个并行条件(CET、RES、END)和对干预的响应(高或低,即 CET_Hi、CET_Lo 等)可视化跨越两个时间点(干预前和干预后)的连续数据点

我想创建一个条形图,在 X 轴上显示每个条件的平均输出,并使用单独的时间条(前和后)。然后,我想用线条覆盖 Pre 和 Post 中的各个主题数据点以连接主题数据点并按颜色对响应进行分组。

我已经使用带有 geom_bar 函数的 ggplot2 成功创建了条形图。我也有 geom_point 按条件覆盖各个点,但无法使位置与时间对齐。

ggplot(Leg_Press_Summary, aes(x=Condition, y=Leg_Press, fill=as.factor(Time))) + 
geom_bar(stat="identity", position=position_dodge()) +
scale_fill_brewer(palette="Blues", name = "Time", labels = c("Pre", "Post")) +
geom_point(data=Phys_Data, aes(x=Condition, y=Leg_Press, colour=Response, fill=as.factor(Time))) +
geom_line(data=Phys_Data, aes(x=Condition, y=Leg_Press, group=Subject)) +
labs(title="Leg Press", x="Condition", y ="Leg Press (kg)")

我希望 geom_points 根据时间定位,但是,对于每个条件,它们的点只是堆叠在 Pre 和 Post 条之间的垂直线上。

我的结果:

My ggplot2 creation

我正在尝试重新创建的图:

Figure I'm trying to recreate

我该如何解决这个问题?

下面包含的数据集,我忘记在原始帖子中包含了。

LegPress
# A tibble: 36 x 5
Subject Time Condition Response Leg_Press

6 1 CET CET_Hi 212.
6 2 CET CET_Hi 300
9 1 CET CET_Lo 350
9 2 CET CET_Lo 370
14 1 CET CET_Hi 330
14 2 CET CET_Hi 450
26 1 CET CET_Hi 180
26 2 CET CET_Hi 250
28 1 CET CET_Lo 230
28 2 CET CET_Lo 275
29 1 CET CET_Lo 330
29 2 CET CET_Lo 325
2 1 RES RES_Hi 142.
2 2 RES RES_Hi 225
16 1 RES RES_Lo 280
16 2 RES RES_Lo 320
19 1 RES RES_Hi 205
19 2 RES RES_Hi 295
27 1 RES RES_Hi 175
27 2 RES RES_Hi 260
31 1 RES RES_Lo 340
31 2 RES RES_Lo 370
32 1 RES RES_Lo 310
32 2 RES RES_Lo 370
8 1 END END_Lo 205
8 2 END END_Lo 250
13 1 END END_Hi 310
13 2 END END_Hi 320
20 1 END END_Hi 200
20 2 END END_Hi 185
24 1 END END_Lo 260
24 2 END END_Lo 270
25 1 END END_Hi 210
25 2 END END_Hi 235
30 1 END END_Lo 250
30 2 END END_Lo 245

最佳答案

它认为这是您要使用 faceting 的情况:

library(tidyverse)  

Phys_Data <- data.frame(stringsAsFactors=FALSE,
Subject = c(6, 6, 9, 9, 14, 14, 26, 26, 28, 28, 29, 29, 2, 2, 16, 16, 19,
19, 27, 27, 31, 31, 32, 32, 8, 8, 13, 13, 20, 20, 24, 24, 25,
25, 30, 30),
Time = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1,
2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2),
Condition = c("CET", "CET", "CET", "CET", "CET", "CET", "CET", "CET", "CET",
"CET", "CET", "CET", "RES", "RES", "RES", "RES", "RES", "RES",
"RES", "RES", "RES", "RES", "RES", "RES", "END", "END", "END",
"END", "END", "END", "END", "END", "END", "END", "END", "END"),
Response = c("CET_Hi", "CET_Hi", "CET_Lo", "CET_Lo", "CET_Hi", "CET_Hi",
"CET_Hi", "CET_Hi", "CET_Lo", "CET_Lo", "CET_Lo", "CET_Lo",
"RES_Hi", "RES_Hi", "RES_Lo", "RES_Lo", "RES_Hi", "RES_Hi",
"RES_Hi", "RES_Hi", "RES_Lo", "RES_Lo", "RES_Lo", "RES_Lo", "END_Lo",
"END_Lo", "END_Hi", "END_Hi", "END_Hi", "END_Hi", "END_Lo",
"END_Lo", "END_Hi", "END_Hi", "END_Lo", "END_Lo"),
Leg_Press = c(212, 300, 350, 370, 330, 450, 180, 250, 230, 275, 330, 325,
142, 225, 280, 320, 205, 295, 175, 260, 340, 370, 310, 370,
205, 250, 310, 320, 200, 185, 260, 270, 210, 235, 250, 245)
)

Phys_Data %>%
mutate(
Time = as.factor(Time),
Response = str_split_fixed(Response, "_", 2)[,2]
) %>%
ggplot(aes(x=Time, y=Leg_Press, fill=Time)) +
facet_wrap(~Condition, strip.position = "bottom") +
geom_col(
data = ~group_by(.x, Time, Condition) %>%
summarize(Leg_Press = mean(Leg_Press)) %>%
ungroup()
) +
scale_fill_brewer(palette="Blues", name = "Time", labels = c("Pre", "Post")) +
geom_point(aes(color=Response)) +
geom_line(aes(color=Response, group=Subject)) +
labs(title="Leg Press", x = "Condition", y ="Leg Press (kg)") +
theme(
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)

reprex package 创建于 2019-09-04 (v0.3.0)

关于r - 如何将geom_bar、geom_point、geom_line合并为一张图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com