Search code examples
rplotly

plot_ly function in R wont't show line in my plot


I can't figure out why my plot_ly function in R won't render lines in my plot, but points of graph are displayed completely fine. Please note that I'd like to use plot_ly function not ggplot or something else. Also I would like to have that selected_col variable to control group_by function.

library(tibble)
library(dplyr)
library(plotly)

date_seq <- seq.Date(as.Date('2023-01-01'), as.Date('2024-01-01'), by="day")

data <- tibble(
  date = sample(date_seq, 100, replace = TRUE),
  id = 1:100,
  principal = round(runif(100, 1000, 10000), 2),
  amount = round(runif(100, 500, 5000), 2),
  char_age = sample(18:70, 100, replace = TRUE),
  char_gender = sample(c("Male", "Female", "Other"), 100, replace = TRUE),
  char_region = sample(c("North", "South", "East", "West"), 100, replace = TRUE),
  char_score = sample(300:850, 100, replace = TRUE)
)

selected_col <- "char_gender"

df <- data %>%
  group_by(date, char = .data[[selected_col]]) %>%
  summarise(principal_mean = mean(principal, na.rm = TRUE)) %>%
  arrange(date) %>%
  mutate(date = as.Date(date))

plot_ly(df, x = ~date, y = ~principal_mean, type = 'scatter', mode = 'lines+markers', color = ~char)

Solution

  • The reason your code only displays points is that you need to sort by using groups = 'drop'. So,

    summarise(principal_mean = mean(principal, na.rm = TRUE), .groups = 'drop')
    

    instead of

    summarise(principal_mean = mean(principal, na.rm = TRUE))
    

    You can do it this way:

    library(tibble)
    library(dplyr)
    library(plotly)
    
    date_seq <- seq.Date(as.Date('2023-01-01'), as.Date('2024-01-01'), by="day")
    
    data <- tibble(
      date = sample(date_seq, 100, replace = TRUE),
      id = 1:100,
      principal = round(runif(100, 1000, 10000), 2),
      amount = round(runif(100, 500, 5000), 2),
      char_age = sample(18:70, 100, replace = TRUE),
      char_gender = sample(c("Male", "Female", "Other"), 100, replace = TRUE),
      char_region = sample(c("North", "South", "East", "West"), 100, replace = TRUE),
      char_score = sample(300:850, 100, replace = TRUE)
    )
    
    selected_col <- "char_gender"
    
    df <- data %>%
      group_by(date, char = .data[[selected_col]]) %>%
      summarise(principal_mean = mean(principal, na.rm = TRUE), .groups = 'drop') %>%
      arrange(char, date) %>%
      mutate(date = as.Date(date))
    
    plot_ly(df, x = ~date, y = ~principal_mean, type = 'scatter', mode = 'lines+markers', color = ~char)
    

    which gives you:

    enter image description here