简体   繁体   中英

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

I've searched for some solutions here but only found it for the case when x variable is numeric. So, for example, I have this dataset:

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

The plot that I have interest (without the abline) is obtained by:

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() 

Static version:

在此处输入图片说明

Now then, how can I obtain exactly the same graph but with an dashed horizontal line on y=0?

From the comments convo, the best you'll (probably) be able to do with ggvis is to add:

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)))

for:

在此处输入图片说明

You can do what you want in ggplot2 though:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM