使用dcast进行2次比较,并按变量进行比较

时间:2016-04-02 04:15:08

标签: r

我正在使用内置" french_fries"包reshape2中的数据,以生成散点图,比较时间= 1到时间= 10和每个变量的facet。到目前为止,我有以下代码:

    molten_fries <- melt(french_fries, id=1:4)

    fry_dat <- molten_fries %>%
    filter(time==c(1,10))

    cast_fries <- dcast(fry_dat, rep + treatment + subject + variable~time,
    mean, value.var="value", na.rm=TRUE)
    colnames(cast_fries) <- c("rep", "treatment", "subject" , "variable",
    "t1","t10")

    ggplot(cast_fries, aes(t1,t10)) + 
    geom_point(na.rm=T) +
    facet_wrap(~variable, nrow=1)

1 个答案:

答案 0 :(得分:0)

代码中只有一个问题:选择时间1&amp; 10,您可以过滤time == 1 & time == 10time %in% c(1,10)。其余的都没问题

library(reshape2)
molten_fries <- melt(french_fries, id=1:4)

library(dplyr)
fry_dat <- molten_fries %>%
  filter(time %in% c(1,10))

cast_fries <- dcast(fry_dat, rep + treatment + subject + variable~time,
                    mean, value.var="value", na.rm=TRUE)
colnames(cast_fries) <- c("rep", "treatment", "subject" , "variable",
                          "t1","t10")
library(ggplot2)
ggplot(cast_fries, aes(t1,t10)) + 
  geom_point(na.rm=T) +
  facet_wrap(~variable, nrow=1)

ggplot_graph