ggvis:当 x 变量是绝对变量时,如何绘制 abline?

ggvis: How can I plot abline when x variable is categorical?

我在这里搜索了一些解决方案,但只找到了 x 变量为数字的情况。因此,例如,我有这个数据集:

read.table(text="         bias           scenery algorithm treatment
 0.0038245022 pi10_40_cens35_40   Method1        T0
 0.0004553608 pi10_40_cens35_40   Method1        T1
 0.0217874958 pi10_40_cens35_40   Method2        T0
 0.0132069964 pi10_40_cens35_40   Method2        T1
 0.0135420058 pi10_40_cens35_40   Method4        T0
 0.0157829608 pi10_40_cens35_40   Method4        T1
 0.0230633621 pi10_40_cens35_40   Method3        T0
 0.0199919247 pi10_40_cens35_40   Method3        T1
 0.0751254422 pi10_40_cens60_65   Method1        T0
 0.0678869679 pi10_40_cens60_65   Method1        T1
 0.1037620465 pi10_40_cens60_65   Method2        T0
 0.0819120457 pi10_40_cens60_65   Method2        T1
 0.0893472639 pi10_40_cens60_65   Method4        T0
 0.0825019605 pi10_40_cens60_65   Method4        T1
 0.1031913181 pi10_40_cens60_65   Method3        T0
 0.1149319836 pi10_40_cens60_65   Method3        T1
 0.0048517692 pi20_30_cens35_40   Method1        T0
 0.0079070239 pi20_30_cens35_40   Method1        T1
 0.0203992390 pi20_30_cens35_40   Method2        T0
 0.0197634214 pi20_30_cens35_40   Method2        T1
 0.0142908113 pi20_30_cens35_40   Method4        T0
 0.0197736578 pi20_30_cens35_40   Method4        T1
 0.0216825265 pi20_30_cens35_40   Method3        T0
 0.0232048669 pi20_30_cens35_40   Method3        T1
 0.0576654516 pi20_30_cens60_65   Method1        T0
 0.0629337504 pi20_30_cens60_65   Method1        T1
 0.0954706388 pi20_30_cens60_65   Method2        T0
 0.0926100594 pi20_30_cens60_65   Method2        T1
 0.0793831867 pi20_30_cens60_65   Method4        T0
 0.0866745996 pi20_30_cens60_65   Method4        T1
 0.1020244131 pi20_30_cens60_65   Method3        T0
 0.1090028420 pi20_30_cens60_65   Method3        T1", 
           header=TRUE, stringsAsFactors=FALSE) -> tbl_sample

我感兴趣的情节(没有abline)是通过以下方式获得的:

tbl_sample %>%
  group_by(algorithm) %>% 
  ggvis(~scenery,~bias, fill=~algorithm,  shape=~treatment) %>% 
  add_legend("fill", properties = legend_props(legend = list(y = 20))) %>% 
  add_legend("shape", properties = legend_props(legend = list(y = 120))) %>%
  add_axis("y",title="bias") %>%
  layer_points() 

静态版本:

那么,如何获得完全相同的图形,但在 y=0 上有水平虚线?

根据 convo 的评论,您(可能)能够使用 ggvis 做的最好的事情是添加:

layer_paths(~x, ~y, stroke:="red", 
            data=data.frame(x=c("pi10_40_cens35_40", "pi20_30_cens60_65"),
                            y=c(0.00 ,0.00)))

对于:

虽然你可以在 ggplot2 中做你想做的事:

library(ggplot2)  

gg <- ggplot()
gg <- gg + geom_point(data=tbl_sample,
                      aes(x=scenery, y=bias, color=algorithm, shape=treatment))
gg <- gg + geom_hline(yintercept=0.00, color="red", linetype="dashed")
gg <- gg + labs(x="Scenery", y="Bias", title=NULL)
gg <- gg + theme_bw()
gg