R 几何点()的位置减淡()

R 几何点()的位置减淡(),r,ggplot2,R,Ggplot2,我正在构建一个所谓的游泳者图,具有连续的X轴、分类的Y轴和不同的点类型元素: require(data.table) require(ggplot2) dt1 <- fread(' patient Time Result patientA 0 Negative patientA 2 Negative patientA 4 Positive patientB 1 Positive patientB 7 Positi

我正在构建一个所谓的游泳者图,具有连续的X轴、分类的Y轴和不同的点类型元素:

require(data.table)
require(ggplot2)

dt1 <- fread('
patient Time    Result
patientA    0     Negative
patientA    2     Negative
patientA    4     Positive
patientB    1     Positive
patientB    7     Positive
patientC    -1  Positive
patientC    2     Negative
patientC    3     Negative
patientC    6     Negative
')

ggplot(dt1, aes(x=Time, y=patient))+
  theme(panel.grid=element_blank())+
  geom_segment(aes(x=xmin,xend=xmax,yend=patient), dt1[,.(xmin=min(Time),xmax=max(Time)),by=patient] )+
  geom_point(aes(shape=Result),size=3, fill='white') + 
  scale_shape_manual(values=c(21,19))

这只是@AllanCameron解决方案的一个变体——增加了一列,但可能看起来更透明、更通用(可以处理2个以上的点!)。我引入了辅助缩放函数
scaleInt()
,它将数字转换为以0为中心的整数向量(例如
1,2
转换为
-1,1
1,2,3
-转换为
-1,0,1
,等等)。然后只需将缩放位置作为一个
组传递即可
美学:

scaleInt <- function(x){
  if (length(x)==1) return(0)
  scaled <- scale(x)[,1]
  ceiling(abs(scaled))*sign(scaled) # <- can this be done simpler?
} 

dt1[, tpG:=scaleInt(seq_len(.N)), by=.(patient,Time)]

ggplot(dt1, aes(x=Time, y=patient)) +
  geom_segment(aes(x=xmin, xend=xmax, yend=patient), dt1[,.(xmin=min(Time), xmax=max(Time)), by=patient]) +
  geom_point(aes(shape=Result, group=tpG), size=3, fill='white', position=position_dodge(width=0.2)) + 
  scale_shape_manual(values = c(21, 19)) +
  theme(panel.grid = element_blank())

scaleInt只是@AllanCameron解决方案的一个变体-增加了一列,但可能看起来更透明、更通用(可以处理2个以上的点!)。我引入了辅助缩放函数
scaleInt()
,它将数字转换为以0为中心的整数向量(例如
1,2
转换为
-1,1
1,2,3
-转换为
-1,0,1
,等等)。然后只需将缩放位置作为一个
组传递即可
美学:

scaleInt <- function(x){
  if (length(x)==1) return(0)
  scaled <- scale(x)[,1]
  ceiling(abs(scaled))*sign(scaled) # <- can this be done simpler?
} 

dt1[, tpG:=scaleInt(seq_len(.N)), by=.(patient,Time)]

ggplot(dt1, aes(x=Time, y=patient)) +
  geom_segment(aes(x=xmin, xend=xmax, yend=patient), dt1[,.(xmin=min(Time), xmax=max(Time)), by=patient]) +
  geom_point(aes(shape=Result, group=tpG), size=3, fill='white', position=position_dodge(width=0.2)) + 
  scale_shape_manual(values = c(21, 19)) +
  theme(panel.grid = element_blank())

scaleInt这里有一种相当扭曲的方法,通过根据点是否重复来标记点,然后将其用作减淡的分组变量

ggplot(dt1,aes(x=时间,y=患者))+
geom_段(aes(x=xmin,xend=xmax,yend=patient),
dt1[,(xmin=min(时间),xmax=max(时间)),by=patient])+
几何点(aes)(形状=结果,组=
因子(粘贴(rev(复制)(rev(交互作用(患者、时间))),
重复(互动(患者、时间)),
级别=c(“假-真”、“假-假”、“真-假”),
尺寸=3,填充=白色,
位置=位置减淡(宽度=0.1))+
比例\形状\手册(值=c(21,19))+
主题(panel.grid=element\u blank())


我认为这和你想要的输出图是一样的。

这里有一种相当扭曲的方法,通过根据点是否重复来标记点,然后将其用作减淡的分组变量

ggplot(dt1,aes(x=时间,y=患者))+
geom_段(aes(x=xmin,xend=xmax,yend=patient),
dt1[,(xmin=min(时间),xmax=max(时间)),by=patient])+
几何点(aes)(形状=结果,组=
因子(粘贴(rev(复制)(rev(交互作用(患者、时间))),
重复(互动(患者、时间)),
级别=c(“假-真”、“假-假”、“真-假”),
尺寸=3,填充=白色,
位置=位置减淡(宽度=0.1))+
比例\形状\手册(值=c(21,19))+
主题(panel.grid=element\u blank())


我想这和你想要的输出图是一样的。

我们可以用
粘贴(患者,时间)
来代替
交互(患者,时间)
,对吗一般来说有什么区别吗?(我想
交互()的性能会更好。
)我使用了您的答案来制作一个更容易阅读的解决方案。再次感谢!!!我们不需要交互(患者,时间)
而只需要粘贴(患者,时间)
,对吗一般来说有什么区别吗?(我想
交互()的性能会更好。
)我使用了您的答案来制作一个更容易阅读的解决方案。再次感谢!!!