连接抖动点的线-多组躲避 [英] Lines connecting jittered points - dodging by multiple groups

查看:46
本文介绍了连接抖动点的线-多组躲避的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将x轴上两种不同方法( measure )的测量值之间的抖动点连接起来.这些测量通过先证者( a )相互关联,可以分为两个主要组,即患者( pat )和对照( ctr ),我的df是这样的:

  set.seed(1)df <-data.frame(a = rep(paste0("id","_"",1:20),每个= 2),值= sample(1:10,40,rep = TRUE),小节= rep(c("a","b"),20),组= rep(c("pat","ctr"),每个= 2,10)) 

我尝试过

 库(ggplot2)ggplot(df,aes(度量,值,填充=组))+geom_point(position = position_jitterdodge(jitter.width = 0.1,jitter.height = 0.1,dodge.width = 0.75),形状= 1)+geom_line(aes(group = a),position = position_dodge(0.75)) 

如果您还想抖动,也可以手动将其添加到x和y变量中.

  df = transform(df,dmeasure = ifelse(group =="ctr",抖动(as.numeric(measure)-.25,.1),抖动(as.numeric(measure)+ .25,.1)),jvalue =抖动(值,数量= .1))ggplot(df,aes(x =度量,y =右值))+geom_blank()+geom_point(aes(x = dmeasure),shape = 1)+geom_line(aes(group = a,x = dmeasure)) 

I try to connect jittered points between measurements from two different methods (measure) on an x-axis. These measurements are linked to one another by the probands (a), that can be separated into two main groups, patients (pat) and controls (ctr), My df is like that:

set.seed(1)
df <- data.frame(a = rep(paste0("id", "_", 1:20), each = 2),
                 value = sample(1:10, 40, rep = TRUE),
                 measure = rep(c("a", "b"), 20), group = rep(c("pat", "ctr"), each = 2,10))

I tried

library(ggplot2)
ggplot(df,aes(measure, value, fill = group)) + 
  geom_point(position = position_jitterdodge(jitter.width = 0.1, jitter.height = 0.1,
                                             dodge.width = 0.75), shape = 1) +
  geom_line(aes(group = a), position = position_dodge(0.75))

Created on 2020-01-13 by the reprex package (v0.3.0)

I used the fill aesthetic in order to separate the jittered dots from both groups (pat and ctr). I realised that when I put the group = a aesthetics into the ggplot main call, then it doesn't separate as nicely, but seems to link better to the points.

My question: Is there a way to better connect the lines to the (jittered) points, but keeping the separation of the two main groups, ctr and pat?

Thanks a lot.

解决方案

The big issue you are having is that you are dodging the points by only group but the lines are being dodged by a, as well.

To keep your lines with the axes as is, one option is to manually dodge your data. This takes advantage of factors being integers under the hood, moving one level of group to the right and the other to the left.

df = transform(df, dmeasure = ifelse(group == "ctr", 
                                     as.numeric(measure) - .25,
                                     as.numeric(measure) + .25 ) )

You can then make a plot with measure as the x axis but then use the "dodged" variable as the x axis variable in geom_point and geom_line.

ggplot(df, aes(x = measure, y = value) ) +
     geom_blank() +
     geom_point( aes(x = dmeasure), shape = 1 ) +
     geom_line( aes(group = a, x = dmeasure) )

If you also want jittering, that can also be added manually to both you x and y variables.

df = transform(df, dmeasure = ifelse(group == "ctr", 
                                     jitter(as.numeric(measure) - .25, .1),
                                     jitter(as.numeric(measure) + .25, .1) ),
               jvalue = jitter(value, amount = .1) )

ggplot(df, aes(x = measure, y = jvalue) ) +
     geom_blank() +
     geom_point( aes(x = dmeasure), shape = 1 ) +
     geom_line( aes(group = a, x = dmeasure) )

这篇关于连接抖动点的线-多组躲避的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆