Combining geom_point and geom_line with position_jitterdodge for two grouping factors

后端 未结 2 1732
无人共我
无人共我 2020-12-11 19:26

I\'ve tried several suggestions based on several posts on here, as well as reading through the ggplot2 documentation, but this question is slightly different, and I haven\'t

相关标签:
2条回答
  • 2020-12-11 20:00

    You can use the interaction between d and b:

    p <- ggplot(temp, aes(x=interaction(d, b), y=c, fill=d, colour=d))+ theme_classic()+
        geom_point()
    p + geom_line(aes(group=a),colour=1)
    

    with correct x axis. Convert the x to numeric and set new labels

    p <- ggplot(temp, aes(x=as.numeric(interaction(d,b)), y=c, fill=d, colour=d))+ theme_classic()+
         geom_point()
    p <- p + geom_line(aes(group=a),colour=1)
    p +  scale_x_continuous(breaks = c(1.5,3.5,5.5), labels = levels(temp$b))
    

    0 讨论(0)
  • 2020-12-11 20:14

    One possible solution - specifying jitter values manualy:

    library(ggplot2)
    
    a <- c(1,2,3,4,5,6,1,2,3,4,5,6)
    # b <- c("loss", "draw", "win", "draw", "loss", "win", "loss", "draw", "win", "draw", "loss", "win")
    b <- c(2, 1, 3, 1, 2, 3, 2, 1, 3, 1, 2, 3)
    c <- c(2, 3, 5, 4, 4, 5, 4, 4, 3, 5, 2, 4)
    d <- c(rep("x", 6), rep("y", 6))
    temp <- data.frame(a,b,c,d)
    
    set.seed(2016)
    jitterVal <- runif(12, max = 0.25)
    jitterVal <- jitterVal * ifelse(temp$d == "x", -1, +1)
    
    ggplot(temp, aes(x = b + jitterVal, y = c, fill = d, colour = d)) +
      geom_point() +
      geom_line(aes(group = a)) +
      scale_x_continuous(breaks = c(1, 2, 3), labels = c("draw", "loss", "win")) +
      xlab(NULL) +
      expand_limits(x = c(0.5, 3.5))
    
    0 讨论(0)
提交回复
热议问题