1

This follows up to this post.

I have xy data I'd like to generate a scatter plot for using plotly. I want to have polygons surrounding clusters of the points.

Data:

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),z=rnorm(50,mean=i,sd=1),cluster=i)))

Creating a data.frame with the coordinates of the polygons:

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

I want to plot it all where I color the points by the df$z values but I'd also like to modify the title of the colorbar.

Here's what I'm trying

df$cluster <- ordered(as.factor(df$cluster))
clusters <- unique(df$cluster)


clusters.plot <- plot_ly()
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,
               name = paste0("Cluster ",l),
               colors=grDevices::colorRamp(c("darkblue","white","darkred")),
               line=list(width=2,color="black"),
               fillcolor='transparent', 
               hoverinfo = "none",
               showlegend = FALSE,
               inherit = FALSE)  


clusters.plot <- clusters.plot %>% 
  add_trace(data=df, x= ~x,y= ~y,color= ~z, colors=grDevices::colorRamp(c("darkblue","white","darkred")),
            type='scatter',mode="markers",
            marker=list(size=10)) %>% 
  layout(xaxis=list(title="X",
                    zeroline=F),
         yaxis=list(title="Y",
                    zeroline=F)) %>% colorbar(limits=c(min(df$z),max(df$z)),len=0.4,title="my legend")

which gives: enter image description here

The problem is that the polygons become points rather than lines, as clusters.plot before adding the points trace is: enter image description here

This happens only when the

colorbar(limits=c(min(df$z),max(df$z)),len=0.4,title="my legend")

is added. Removing it gives: enter image description here

So my question is how can I edit the legend's title without affecting the polygons?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
dan
  • 6,048
  • 10
  • 57
  • 125
  • 2
    There are known issues with the `colorbar()ยด: https://github.com/ropensci/plotly/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+colorbar โ€“ hplieninger Feb 13 '18 at 07:54
  • you can pass the len and title without a problem, but not the `limits=c(min(df$z),max(df$z))` โ€“ MLavoie Feb 13 '18 at 07:56
  • 1
    I have added this case to [plotly issue #1196: `colorbar()` and `hide_colorbar()` has unexpected side-effect on line traces](https://github.com/ropensci/plotly/issues/1196) . The fact that the `limits` seems to be the only argument to `colorbar()` that triggers the behavior is interesting, that actually might help narrow it down. If you come across any other quirks, adding them as a comment on that issue would be helpful. โ€“ Matt Summersgill Feb 13 '18 at 11:44

0 Answers0