GgVis: creating a graph with multiple layers in different datasets

I am trying to play the ggplot2 plot using ggvis. The plot is aimed at representing the coordinates of points (from the analysis of correspondence) together with standard dispersion ellipses of clusters (hclust).


TL; DR

I would like to make a ggvis graph with multiple layers based on multiple datasets. Thus, the functional / pipe approach stops me from grouping one of the layers and not the other.

All (briefly commented) code is: https://gist.github.com/RCura/a135446cda079f4fbc10


Here is the code to create the data:

a <- rnorm(n = 100, mean = 50, sd = 5) b <- rnorm(n = 100, mean = 50, sd = 5) c <- rnorm(n = 100, mean = 50, sd = 5) mydf <- data.frame(A = a, B = b, C = c, row.names = c(1:100)) library(ade4) myCA <- dudi.coa(df = mydf,scannf = FALSE, nf = 2) myDist <- dist.dudi(myCA, amongrow = TRUE) myClust <- hclust(d = myDist, method = "ward.D2") myClusters <- cutree(tree = myClust, k = 3) myCAdata <- data.frame(Axis1 = myCA$li$Axis1, Axis2 = myCA$li$Axis2, Cluster = as.factor(myClusters)) library(ellipse) # Compute Standard Deviation Ellipse df_ellipse <- data.frame() for(g in levels(myCAdata$Cluster)){ df_ellipse <- rbind(df_ellipse, cbind(as.data.frame( with(myCAdata[myCAdata$Cluster==g,], ellipse(cor(Axis1, Axis2), level=0.7, scale=c(sd(Axis1),sd(Axis2)), centre=c(mean(Axis1),mean(Axis2))))), Cluster=g)) } 

I can build this via ggplot2:

 library(ggplot2) myPlot <- ggplot(data=myCAdata, aes(x=Axis1, y=Axis2,colour=Cluster)) + geom_point(size=1.5, alpha=.6) + geom_vline(xintercept = 0, colour="black",alpha = 0.5, linetype = "longdash" ) + geom_hline(xintercept = 0, colour="black", alpha = 0.5, linetype = "longdash" ) + geom_path(data=df_ellipse, aes(x=x, y=y,colour=Cluster), size=0.5, linetype=1) myPlot 

enter image description here

But I can not find how to build this with ggvis.

I can build 2 different layers:

 library(ggvis) all_values <- function(x) { paste0(names(x), ": ", format(x), collapse = "<br />")} ggDF <- myCAdata ggDF$name <- row.names(ggDF) ## Coordinates plot myCoordPlot <- ggvis(x = ~Axis1, y = ~Axis2, key := ~name, data = ggDF) %>% layer_points(size := 15, fill= ~Cluster, data = ggDF) %>% add_tooltip(all_values, "hover") myCoordPlot 

enter image description here

Graph of ellipses (without tooltip tooltip)

  myEllPlot <- ggvis(data = df_ellipse, x = ~x, y = ~ y) %>% group_by(Cluster) %>% layer_paths(x= ~x, y= ~y, stroke = ~Cluster, strokeWidth := 1) myEllPlot 

enter image description here

But when I want to build 2 layers on the same plot:

  myFullPlot <- ggvis(data = df_ellipse, x = ~x, y = ~ y) %>% layer_paths(x= ~x, y= ~y, stroke = ~Cluster, strokeWidth := 1) %>% layer_points(x = ~Axis1, y= ~Axis2, size := 15, fill= ~Cluster, data = ggDF) %>% add_tooltip(all_values, "hover") myFullPlot 

enter image description here

Ellipses are not grouped, so the color does not fit, and the ellipses are not divided. If I try to group my ellipses, this will not work: group_by is only required for layer_paths, and this will ruin layer_points.

Any idea how to make this work? And sorry for this very long post, but I tried to make this work for hours: /

+8
r ggplot2 ggvis
source share
1 answer

The problem is that when you try to combine the two, you are not group_by Cluster in the ellipsis dataset. To do this, you need to do the following:

 myFullPlot <- ggvis(data = df_ellipse, x = ~x, y = ~ y) %>% group_by(Cluster) %>% layer_paths(stroke = ~Cluster, strokeWidth := 1) %>% layer_points(x = ~Axis1, y= ~Axis2, size := 15, fill= ~Cluster, data = ggDF) myFullPlot 

enter image description here

And so you get the right graph!

PS I assume that there is some randomness in creating the data, because I have a different data set than yours.

+8
source share

All Articles