gpt4 book ai didi

R:箱线图:在重复测量的情况下在每个主题之间画线

转载 作者:行者123 更新时间:2023-12-02 00:08:56 24 4
gpt4 key购买 nike

我有数据,其中我对 4 个不同阶段的相同对象进行了测量。我的目标是制作一个图表,其中既有箱线图,也有每个主题的点,每个阶段之间用一条线连接每个主题。最终结果将是将下面的两个图组合成一个最终图

library(ggplot2)
df_original = data.frame(study_id = c("id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8", "id9", "id10"),
val_stage1 = runif(10, 5.0, 6.0),
val_stage2 = runif(10, 5.0, 6.5),
val_stage3 = runif(10, 4.7, 5.8),
val_stage4 = runif(10, 5.5, 7.0))
df_original
df_plot1 = data.frame(group = "stage1", value = df_original[, "val_stage1"], ID = df_original$study_id)
df_plot2 = data.frame(group = "stage2", value = df_original[, "val_stage2"], ID = df_original$study_id)
df_plot3 = data.frame(group = "stage3", value = df_original[, "val_stage3"], ID = df_original$study_id)
df_plot4 = data.frame(group = "stage4", value = df_original[, "val_stage4"], ID = df_original$study_id)

plot_data = rbind(df_plot1,
df_plot2,
df_plot3,
df_plot4)
ggplot(plot_data, aes(x=group,
y=value,
fill=group
)
) +
geom_boxplot(outlier.shape = NA) +
geom_point()

boxplot

library(lattice)
# source https://publicifsv.sund.ku.dk/~jufo/courses/rm2017/plotRrepeated.pdf
xyplot(value ~ group, group = ID, data = plot_data, type = "b")

repeated measures with lines between stage

最佳答案

一个 tidyverse 方法看起来像这样:

mylabs <- levels(plot_data$group)
library(tidyverse)
plot_data %>%
ggplot(aes(x = as.numeric(group), y = value)) +
geom_boxplot(aes(group = group, fill = group), outlier.shape = NA) +
geom_point(aes(color = ID)) +
geom_line(aes(color = ID)) +
theme(legend.position = "none") +
labs(x = "Group") +
scale_x_continuous(breaks = 1:4, labels = mylabs)

enter image description here

关于R:箱线图:在重复测量的情况下在每个主题之间画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59693411/

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