2015-09-07 35 views
2

我在这里搜索了一些解决方案,但只发现它的情况下,当x变量是数字。因此,举例来说,我有这样的数据集:ggvis:如何在x变量为分类时绘制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() 

静态版本:

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)该地块由获得

enter image description here

现在,我怎样才能获得完全相同的图形,但在y = 0上有水平虚线?

+0

尝试'geom_hline(yintercept = 0,lty = 1)'或者如果需要'geom_hline(yintercept = 0,lty = 1,aes(group = 1))'? –

+0

oops。 'aes(group = 1)'可能仍然有效? –

+0

这看起来像是最好的答案:https://groups.google.com/forum/#!topic/ggvis/csYp7r3BwSA –

回答

2

从评论康沃,最好你会(可能)能够与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))) 

为:

enter image description here

你可以做你想在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 

enter image description here

+0

太棒了!我肯定会使用这个。猜猜它现在仍然值得学习ggplot2。谢谢。 – jbrettas

+0

是'group_by(tbl_sample,algorithm)'这里真的需要吗?只会'tbl_sample'工作...? –

+0

你是对的@BenBolker。我只是从我的本地ggvis行做了一个懒惰的复制/粘贴。 – hrbrmstr

相关问题