1

我有一个数据框,其中包含来自相同主题的不同时间点(“时间 0”和“时间 3”)的配对样本。如何生成散点图,其中 x 坐标对应于“时间 0”,y 坐标对应于每个主题的“时间 3”。

subject = c(1,1,2,2,3,3)
time = c(0,3,0,3,0,3)
dependent_variable = c(1,5,4,12,3,9)
df = data.frame(subject, time, dependent_variable)
4

1 回答 1

3

为了达到您想要的结果,您可以使用例如重塑您的数据tidy::pivot_wider

subject = c(1,1,2,2,3,3)
time = c(0,3,0,3,0,3)
dependent_variable = c(1,5,4,12,3,9)
df = data.frame(subject, time, dependent_variable)

library(ggplot2)
library(tidyr)

df_wide <- df %>% 
  pivot_wider(names_from = time, values_from = dependent_variable, names_prefix = "time")

ggplot(df_wide, aes(time0, time3, color = factor(subject))) +
  geom_point()

于 2020-12-30T23:40:41.910 回答