Search code examples
rggplot2popupr-plotlyggplotly

Error in gsub("\n", br(), a, fixed = TRUE)


I'm trying to use ggplot to create a scatter plot of player data with a pop up that includes the players name. The data is from here. My code is below.

x <- Player_Stats %>%
  ggplot(aes(x=PTS, y=`ThreeP%`,color=Tm, text=paste("Player:", Player)))+
  geom_point()

ggplotly(x, tooltip = "text")

When I run the code I receive an error that says Error in gsub("\n", br(), a, fixed = TRUE) : input string 16 is invalid in this locale.

My current popup just includes PTS, 3p% and Team. How can I add the player name to the popup?


Solution

  • I'm not sure what caused your error, but I went to your data source link and selected the "Get table as CSV (for Excel)" option from "Share & Export". I highlighted all the resulting table text, and copied to excel. I then did "Text to columns" and split the data by the comma delimiters. I then saved the file as a .csv extension. I then ran your code. It didn't work initially because the ThreeP% variable was named 3P% in the .csv file. I renamed it, and then your code worked fined thereafter, producing your desired output. Maybe your code is fine, but the input data, isn't working somehow? Hard to say without precisely reproducing your input.

    library(tidyverse)
    library(plotly)
    
    data <- read_csv("player_stats.csv")
    
    colnames(data)
    colnames(data)[14] <- "ThreeP%"
    
    x <- data %>%
      ggplot(aes(x=PTS, y=`ThreeP%`, color=Tm, text=paste("Player:", Player)))+
      geom_point()
    
    ggplotly(x, tooltip = "text")
    

    Output