2017-12-22 10 views
1

Ich habe 5 Cluster von x,y Daten, die ich mit R 's plotly plotten.Hinzufügen eines Polygons zu einem Streudiagramm

Hier sind die Daten:

set.seed(1) 
df <- do.call(rbind,lapply(seq(1,20,4),function(i) data.frame(x=rnorm(50,mean=i,sd=1),y=rnorm(50,mean=i,sd=1),cluster=i))) 

Hier ist das Grundstück:

library(plotly) 
clusters.plot <- plot_ly(marker=list(size=10),type='scatter',mode="markers",x=~df$x,y=~df$y,color=~df$cluster,data=df) %>% hide_colorbar() %>% layout(xaxis=list(title="X",zeroline=F),yaxis=list(title="Y",zeroline=F)) 

clusters.plot 

enter image description here

Jetzt jedem Cluster Ich erstelle Polygone um:

#helper functions: 
library(data.table) 
library(grDevices) 

splinesPolygon <- function(xy,vertices,k=3, ...) 
{ 
    # Assert: xy is an n by 2 matrix with n >= k. 
    # Wrap k vertices around each end. 
    n <- dim(xy)[1] 
    if (k >= 1) { 
    data <- rbind(xy[(n-k+1):n,], xy, xy[1:k, ]) 
    } else { 
    data <- xy 
    } 
    # Spline the x and y coordinates. 
    data.spline <- spline(1:(n+2*k), data[,1], n=vertices, ...) 
    x <- data.spline$x 
    x1 <- data.spline$y 
    x2 <- spline(1:(n+2*k), data[,2], n=vertices, ...)$y 
    # Retain only the middle part. 
    cbind(x1, x2)[k < x & x <= n+k, ] 
} 

clustersPolygon <- function(df) 
{ 
    dt <- data.table::data.table(df) 
    hull <- dt[,.SD[chull(x,y)]] 
    spline.hull <- splinesPolygon(cbind(hull$x,hull$y),100) 
    return(data.frame(x=spline.hull[,1],y=spline.hull[,2],stringsAsFactors=F)) 
} 

library(dplyr) 
polygons.df <- do.call(rbind,lapply(unique(df$cluster),function(l) clustersPolygon(df=dplyr::filter(df,cluster == l)) %>% dplyr::rename(polygon.x=x,polygon.y=y) %>% dplyr::mutate(cluster=l))) 

Und jetzt möchte ich sie zu clusters.plot hinzufügen.

dachte ich, das tun würde:

clusters <- unique(df$cluster) 
for(l in clusters) clusters.plot <- clusters.plot %>% add_polygons(x=dplyr::filter(polygons.df,cluster == l)$polygon.x,y=dplyr::filter(polygons.df,cluster == l)$polygon.y,line=list(width=2,color="black"),fillcolor='transparent') 

Aber ich bin immer diese Fehlermeldung:

Error: Columns `x`, `y` must be length 1 or 250, not 54, 54 

250 ist nrow(df) und 54 ist nrow(dplyr::filter(polygonss.df,cluster == 1))

Jede Idee?

Antwort

2

Versuchen Sie folgendes:

for(l in clusters) clusters.plot <- clusters.plot %>% 
add_polygons(x=dplyr::filter(polygons.df,cluster == l)$polygon.x, 
       y=dplyr::filter(polygons.df,cluster == l)$polygon.y, 
       line=list(width=2,color="black"), 
       fillcolor='transparent', inherit = FALSE) 

enter image description here

Verwandte Themen