R 根据输入数据设置geom_段的可变位置

R 根据输入数据设置geom_段的可变位置,r,ggplot2,R,Ggplot2,我试图在绘图中添加一条垂直线,作为基准方法性能的参考线。但是,我不知道如何根据我的数据定义y-值 以下是一个具有所需绘图的完全可复制的示例: library(tidyverse) set.seed(16) tibble("X"=rep(c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B"),3)

我试图在绘图中添加一条垂直线,作为基准方法性能的参考线。但是,我不知道如何根据我的数据定义
y
-值

以下是一个具有所需绘图的完全可复制的示例:

library(tidyverse)
set.seed(16)
tibble("X"=rep(c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B"),3), 
       "Decision"=c(rep("Type 1",6),rep("Type 2",6),rep("Type 3",6)), 
       "Outcome"=c(rnorm(n=6,mean=50,sd=5),rnorm(n=6,mean=30,sd=5),rnorm(n=6,mean=20,sd=5))) %>%
  ggplot(., aes(X, Outcome, color=Decision, shape=Decision, size=2)) + geom_point(stroke=2, alpha = 0.8) + 
  scale_x_discrete(limits=c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B")) +
  geom_segment(aes(x = 1, y = 52.4, xend = 3, yend = 52.4), color="black", linetype="dashed", size=1) + 
  geom_segment(aes(x = 4, y = 42.8, xend = 6, yend = 42.8), color="black", linetype="dashed", size=1) 

应将
参考.A的
决定
类型1
结果
,与
1.A
2.A
方法进行比较,并相应地与
B
方法进行比较<代码>第2类
第3类
不应使用此类基准线表示


在本例中,我已经手动设置了
y
的值,但我的数据经常更改,因此最好有一个灵活的解决方案。有人知道这样做的方法吗?

过滤
几何图形段的数据(您只需要一个):

这将使用
%>%定义函数序列的技巧,因为您需要将函数作为数据参数传递,因为您需要导入数据

或者:

d <- tibble("X"=rep(c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B"),3), 
       "Decision"=c(rep("Type 1",6),rep("Type 2",6),rep("Type 3",6)), 
       "Outcome"=c(rnorm(n=6,mean=50,sd=5),rnorm(n=6,mean=30,sd=5),rnorm(n=6,mean=20,sd=5)))

ggplot(d , aes(X, Outcome, color=Decision, shape=Decision, size=2)) + 
  geom_point(stroke=2, alpha = 0.8) + 
  scale_x_discrete(limits=c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B")) +
  geom_segment(
    aes(x = c(1, 4), y = Outcome, xend = c(3, 6), yend = Outcome), 
    data = filter(d, Decision == 'Type 1', X %in% c('Reference.A', 'Reference.B')),
    color="black", linetype="dashed", size=1
  )
d
d <- tibble("X"=rep(c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B"),3), 
       "Decision"=c(rep("Type 1",6),rep("Type 2",6),rep("Type 3",6)), 
       "Outcome"=c(rnorm(n=6,mean=50,sd=5),rnorm(n=6,mean=30,sd=5),rnorm(n=6,mean=20,sd=5)))

ggplot(d , aes(X, Outcome, color=Decision, shape=Decision, size=2)) + 
  geom_point(stroke=2, alpha = 0.8) + 
  scale_x_discrete(limits=c("Reference.A", "1.A", "2.A", "Reference.B", "1.B", "2.B")) +
  geom_segment(
    aes(x = c(1, 4), y = Outcome, xend = c(3, 6), yend = Outcome), 
    data = filter(d, Decision == 'Type 1', X %in% c('Reference.A', 'Reference.B')),
    color="black", linetype="dashed", size=1
  )