【问题标题】:Split dataframe into two groups将数据框分成两组
【发布时间】:2015-07-11 07:51:58
【问题描述】:

我已经模拟了这个data.frame

library(plyr); library(ggplot2)
count <- rev(seq(0, 500, 20))
tide <- seq(0, 5, length.out = length(count))
df <- data.frame(count, tide)

count_sim <- unlist(llply(count, function(x) rnorm(20, x, 50)))
count_sim_df <- data.frame(tide=rep(tide,each=20), count_sim)

而且可以这样绘制:

ggplot(df, aes(tide, count)) + geom_jitter(data = count_sim_df, aes(tide, count_sim), position = position_jitter(width = 0.09)) + geom_line(color = "red")

我现在想将count_sim_df 分成两组:highlow。当我绘制分割count_sim_df 时,它应该看起来像这样(绿色和蓝色的所有内容都经过了photoshop)。我发现棘手的一点是在highlow 之间在tide 的中间值附近重叠。

这就是我想将count_sim_df 分成高低的方式:

  • count_sim_df 的一半分配给high,将count_sim_df 的一半分配给low
  • 重新分配count 的值,以在tide 的中间值附近创建highlow 之间的重叠

【问题讨论】:

    标签: r ggplot2 dataframe visualization


    【解决方案1】:

    这是我修改后的建议。我希望它有所帮助。

    middle_tide <- mean(count_sim_df$tide)
    hilo_margin <- 0.3
    middle_df <- subset(count_sim_df,tide > (middle_tide * (1 - hilo_margin)))
    middle_df <- subset(middle_df, tide < (middle_tide * (1 + hilo_margin)))
    upper_df <- count_sim_df[count_sim_df$tide > (middle_tide * (1 + hilo_margin)),]
    lower_df <- count_sim_df[count_sim_df$tide < (middle_tide * (1 - hilo_margin)),]
    idx <- sample(2,nrow(middle_df), replace = T)
    count_sim_high <- rbind(middle_df[idx==1,], upper_df)
    count_sim_low <- rbind(middle_df[idx==2,], lower_df)
    p <- ggplot(df, aes(tide, count)) + 
       geom_jitter(data = count_sim_high, aes(tide, count_sim), position = position_jitter(width = 0.09), alpha=0.4, col=3, size=3) + 
       geom_jitter(data = count_sim_low, aes(tide, count_sim), position = position_jitter(width = 0.09), alpha=0.4, col=4, size=3) + 
       geom_line(color = "red")
    

    我可能还没有完全理解您拆分高低的过程,尤其是您所说的“重新分配计数值”的意思。在这种情况下,我在tide 的中间值周围定义了一个 30% 的重叠区域,并将该过渡区域内的一半点随机分配给“高”集,另一半分配给“低”集。

    【讨论】:

    • 已编辑问题以更清楚地说明如何创建重叠
    【解决方案2】:

    这是一种使用相对较少的代码和仅使用基础 R 生成示例数据集和分组的方法:

    library(ggplot2)
    count <- rev(seq(0, 500, 20))
    tide <- seq(0, 5, length.out = length(count))
    df <- data.frame(count, tide)
    
    count_sim_df <- data.frame(tide = rep(tide,each=20),
                               count = rnorm(20 * nrow(df), rep(count, each = 20), 50))
    margin <- 0.3
    count_sim_df$`tide level` <-
      with(count_sim_df,
        factor((tide >= quantile(tide, 0.5 + margin / 2) |
               (tide >= quantile(tide, 0.5 - margin / 2) & sample(0:1, length(tide), TRUE))),
               labels = c("Low", "High")))
    ggplot(df, aes(x = tide, y = count)) +
      geom_line(colour = "red") +
      geom_point(aes(colour = `tide level`), count_sim_df, position = "jitter") +
      scale_colour_manual(values = c(High = "green", Low = "blue"))
    

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      相关资源
      最近更新 更多