Scatterplot based on 2 categorical variables using ggvis package in R

142 Views Asked by At

enter image description here

I need to get a scatter plot like this based on 2 categorical variables where each variable has 2 levels. I am using ggvis package in R.

This is my code so far

 salab<- read.table("http://users.stat.ufl.edu
/~rrandles/sta4210/Rclassnotes/data/textdatasets/KutnerData/
Chapter%2022%20Data%20Sets/CH22TA06.txt", quote="\"", comment.char="")


    salab %>% ggvis(~V2, ~V1, fill = ~factor(V3*V4)) %>% layer_points() 

enter image description here

Which is incorrect because i need 4 factors combinations. Can anyone help me to figure out what modification should i do ?

2

There are 2 best solutions below

0
DanY On BEST ANSWER

I think you need factor(V3):factor(V4) instead of factor(V3*V4):

salab %>% 
  ggvis(~V2, ~V1, fill = ~ factor(V3):factor(V4)) %>% 
  layer_points() 
0
Iman On

An alternative:

salab$`V3*V4`<-paste0("V3=",salab$V3,"*","V4=",salab$V4)
salab %>% ggvis(~V2, ~V1, shape = ~`V3*V4`)  %>% layer_points() 

enter image description here