ggVis:在不同数据集上创建多个图层的情节 [英] ggVis : creating a plot with multiple layers on different dataset
问题描述
我试图用ggvis重现一个ggplot2图。
该图旨在表示点的坐标(来自对应分析)及其群集(hclust)标准色散椭圆。
TL; DR
我想根据多个数据集制作一个包含多个图层的ggvis图。
因此,功能/管道方法阻止我将其中一个图层分组,而不是其他分组。
整个(简短评论)的代码在那里: a href =https://gist.github.com/RCura/a135446cda079f4fbc10 =noreferrer> https://gist.github.com/RCura/a135446cda079f4fbc10
以下是创建数据的代码:
a (n = 100,平均= 50,sd = 5)
b < - rnorm(n = 100,平均值= 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.f actor(myClusters))
库(椭圆)#计算标准偏差椭圆
df_ellipse< - data.frame()
for(g (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)),
center = c(mean Axis1),mean(Axis2))))),
Cluster = g))
}
我可以通过ggplot2绘图:
library(ggplot2)
myPlot< ; - ggplot(data = myCAdata,aes(x = Axis1,y = Axis2,color = Cluster))+
geom_point(size = 1.5,alpha = .6)+
geom_vline(xintercept = 0, color =black,alpha = 0.5,linetype =longdash)+
geom_hline(xintercept = 0,color =black,alpha = 0.5,linetype =longdash)+
geom_path数据= df_ellip se,aes(x = x,y = y,color = Cluster),size = 0.5,linetype = 1)
myPlot
但是我找不到如何使用ggvis来绘制这个图。
我可以绘制2个不同的图层:
$ b (x),:,格式(x),格式(x),格式(x) ,collapse =< br />)}
ggDF< - myCAdata
ggDF $ name< - row.names(ggDF)
##坐标图
myCoordPlot< - ggvis(x =〜Axis1,y =〜Axis2,key:=〜name,data = ggDF)%>%
(所有值,悬停)
myCoordPlot
省略图(不需要提示工具提示)
myEllPlot < - ggvis(data = df_ellipse,x =〜x,y =〜y)%>%
group_by(Cluster)%>%
layer_paths(x =〜x,y =〜y,stroke =〜Cluster,strokeWidth:= 1)
myEllPlot
但是,当我想在同一个图上绘制两层时:
myFullPlot< - ggvis(data = df_ellipse, x = y,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
椭圆不分组,所以颜色不适合,而椭圆不分开。
如果我尝试对我的椭圆组进行分组,它不起作用:group_by只被layer_paths需要,并且会弄乱layer_points。
任何想法如何使这项工作?
抱歉,这篇很长的文章,但我一直在努力使这项工作数小时:/ $ / $>
问题在于,当您尝试将两者结合使用时,省略号数据集中的group_by Cluster
不是。你需要做以下的工作:
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
这样你就可以得到你想要的图表!
PS我假设您的数据创建中存在一些随机性,因为我得到的数据集与您的数据集不同。
I'm trying to reproduce a ggplot2 plot using ggvis. The plot aims at representing the coordinates of points (from a Correspondence Analysis) together with their clusters (hclust) Standard Dispersion Ellipses.
TL; DR
I'd like to make a ggvis plot with multiple layers based on multiple datasets. Thus, the functional/pipe approach stops me from grouping one of the layers and not the other.
The whole (briefly commented) code is there : https://gist.github.com/RCura/a135446cda079f4fbc10
Here's the code for creating 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 plot this through 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
But I can't find how to plot this using ggvis.
I can plot the 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
Ellipses plot (no tooltip requested)
myEllPlot <- ggvis(data = df_ellipse, x = ~x, y = ~ y) %>%
group_by(Cluster) %>%
layer_paths(x= ~x, y= ~y, stroke = ~Cluster, strokeWidth := 1)
myEllPlot
But when I want to plot the 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
The ellipses are not grouped, so, the color don't fit, and the ellipses are not separated. If I try to group my Ellipses, it doesn't work: the group_by is only required by the layer_paths, and it mess up the layer_points.
Any idea how to make this work? And sorry for this very long post, but I've been trying to make this work for hours :/
The problem is that when you try to combine the two, you do not group_by Cluster
on the ellipsis dataset. You need to do the following for it to work:
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
And this way you get the graph you want!
P.S. I assume there is some randomness in your data creation because I got a different data set than yours.
这篇关于ggVis:在不同数据集上创建多个图层的情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!