Home > Software engineering >  How to connect pairs of data points in plotly in R
How to connect pairs of data points in plotly in R

Time:12-12

library(plotly)
data <- data.frame(name = c("Ana", "Ana", "Joyce", "Joyce", "Kam", "Kam"),
                   type = c("A", "B", "A", "B", "A", "B"),
                   score1 = c(100, 90, 0, 20, 33, 99),
                   score2 = c(100, 30, 0, 20, 55, 66))
> data
   name type score1 score2
1   Ana    A    100    100
2   Ana    B     90     30
3 Joyce    A      0      0
4 Joyce    B     20     20
5   Kam    A     33     55
6   Kam    B     99     66

I have a dataset where each person has 2 scores for test type A and 2 scores for test type B. I want to connect the test scores for each person with a dashed line.

Here's my code for the plot without the dashed lines:

> plot_ly(data = data,
                       x = ~score1,
                       y = ~score2,
                       hoverinfo = 'text',
                       text = ~I(name),
                       mode = "markers",
                       transforms = list(
                         list(
                           type = 'groupby',
                           groups = data$type,
                           styles = list(
                             list(target = "A", value = list(marker =list(color = 'red'))),
                             list(target = "B", value = list(marker =list(color = 'blue')))
                           )
                         )
                       )) 

The desired output is something like this:

enter image description here

CodePudding user response:

Since you're specifying specific colors in the point plot, it's just going to be easier to create the lines first.

First, for each line segment, you will need a separate trace. There are only three, but I used this method due to its dynamic nature. Your line segments are connected by the data in name, so I used that to control the line trace creation.

plt <- plot_ly()
lapply(1:length(unique(df1$name)), 
       function(j) {
         df2 <- filter(df1, name == unique(df1$name)[j])
         plt <<- plt %>% 
           add_trace(type = "scatter", mode = "lines", data = df2, 
                     x = ~score1, y = ~score2, showlegend = F, hoverinfo = "none",
                     line = list(color = "black", dash = "dash"))
       })

Now that the lines are all documented in plt, let's add that original scatter data to the plot. I've added hovertemplate so that the hover content doesn't include the trace name.

plt %>% 
  add_trace(data = df1, x = ~score1, y = ~score2, hovertext = ~name,
            color = ~type, mode = "markers", showlegend = T,
            hovertemplate = "%{hovertext}<extra></extra>",
            marker = list(color = ifelse(df1$type == "A", "red", "blue")))

enter image description here

  • Related